note 95166 added to simplexmlelement.addChild

From: Date: Wed, 16 Dec 2009 11:47:31 +0000
Subject: note 95166 added to simplexmlelement.addChild
Groups: php.notes 
Request: Send a blank email to php-notes+get-164041@lists.php.net to get a copy of this message
Here's a compilation of above notes we've made to ease the use of simpleXMLElement in our web agency, hope this will help some people arround: <?php /** * extended simpleXMLElement * @class verySimpleXMLElement */ class verySimpleXMLElement extends SimpleXMLElement{ /** * add a child to the current element and return it in the same way as original * simpleXMLElement but without value verification. * @param string $nodeName * @param string $value * @param string $nameSpace * @return verySimpleXMLElement */ public function addChild($nodeName,$value=null,$nameSpace=null){ $node = parent::addChild($nodeName,null,$nameSpace); if( $value !== null) $node->{0} = $value; return $node; } /** * same as addChild method but the value will be put inside a CData section. * @param string $nodeName * @param string $value * @param string $nameSpace * @return verySimpleXMLElement */ public function addCDataChild($nodeName,$value,$nameSpace=null){ $node = $this->addChild($nodeName,null,$nameSpace); return $node->addCData($value); } /** * add a CData section to the current element and return this for method chaining * @param string $cdata * @return $this */ public function addCData($cdata){ $node= dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata)); return $this; } /** * exactly the same as simpleXMLElement::addAttribute() * but return this for method chaining * @param string $nodeName * @param string $value * @param string $nameSpace * @return $this */ public function addAttribute($string,$value,$nameSpace=null){ parent::addAttribute($string,$value,$nameSpace); return $this; } } //-- sample usage: $item = new verySimpleXMLElement('<item />'); $item->addAttribute('attrName','attrValue') ->addChild('child','test value with some special chars &'); $item->addCData('&some cdata&')->addCdataChild('cdataItem','with some cdatas inside'); echo $item->asXml(); ---- Server IP: 195.221.21.36 Probable Submitter: 217.128.159.36 ---- Manual Page -- http://www.php.net/manual/en/simplexmlelement.addChild.php Edit -- https://master.php.net/note/edit/95166 Del: integrated -- https://master.php.net/note/delete/95166/integrated Del: useless -- https://master.php.net/note/delete/95166/useless Del: bad code -- https://master.php.net/note/delete/95166/bad+code Del: spam -- https://master.php.net/note/delete/95166/spam Del: non-english -- https://master.php.net/note/delete/95166/non-english Del: in docs -- https://master.php.net/note/delete/95166/in+docs Del: other reasons-- https://master.php.net/note/delete/95166 Reject -- https://master.php.net/note/reject/95166 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164041) next »