note 107409 added to simplexmlelement.addchild
| From: | jasen at treshna dot com | Date: | Tue, 07 Feb 2012 03:51:17 +0000 |
| Subject: | note 107409 added to simplexmlelement.addchild | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185888@lists.php.net to get a copy of this message | ||
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 & Chips");
you can do
$newnode=$menuitem->addchild("dish");
$newode[0]="Fish & Chips";
----
Server IP: 202.89.58.1
Probable Submitter: 123.255.47.113
----
Manual Page -- http://www.php.net/manual/en/simplexmlelement.addchild.php
Edit -- https://master.php.net/note/edit/107409
Del: integrated -- https://master.php.net/note/delete/107409/integrated
Del: useless -- https://master.php.net/note/delete/107409/useless
Del: bad code -- https://master.php.net/note/delete/107409/bad+code
Del: spam -- https://master.php.net/note/delete/107409/spam
Del: non-english -- https://master.php.net/note/delete/107409/non-english
Del: in docs -- https://master.php.net/note/delete/107409/in+docs
Del: other reasons-- https://master.php.net/note/delete/107409
Reject -- https://master.php.net/note/reject/107409
Search -- https://master.php.net/manage/user-notes.php