note 95166 deleted from simplexmlelement.addChild by danbrown

From: Date: Wed, 16 Dec 2009 15:03:01 +0000
Subject: note 95166 deleted from simplexmlelement.addChild by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-164051@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- 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();

« previous php.notes (#164051) next »