note 107409 deleted from simplexmlelement.addchild by danbrown

From: Date: Thu, 10 May 2012 21:17:43 +0000
Subject: note 107409 deleted from simplexmlelement.addchild by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187870@lists.php.net to get a copy of this message
Note Submitter: jasen at treshna dot com ---- Jerikojerk's example only works with nodes containing only text, if you run it you will see that in the resulting XML the attributes "pos=..."have been dropped. if you try it with nodes containing other nodes the contained nodes are just ignored and their content is dropped. It appears that there is an implicit cast to string at the assignment. using this simplexml DOM method seems to be a waste of time. $element->{"childname"}[]="stringcontent"; seems to work better in all cases. done this way '&','<', and '>' are escaped correctly, but you still can't graft trees in this way, only single #text nodes. I'm guessing that some sort of recursion like Alex Feraud uses in his extension is needed to do a full tree grafting operation using simplexml. as felipenmoura demonstrates it is however relatively simple to work in a top-down direction, growing the XML tree from the root instead of grafting branches on. addChild gets you a (writable) referenece to the child which is useful, but you had best remeber to escape your content when using addchild or instead the content to index 0 of the new node reference eg: given $menuitem is a SimpleXMLElement instead of $newnode=$menuitem->addchild("dish",'Fish &amp; Chips"); you can do $newnode=$menuitem->addchild("dish"); $newode[0]="Fish & Chips";

« previous php.notes (#187870) next »