I have two XML trees and would like to add one tree as a leaf to the other one.
Apparently:
$tree2->addChild('leaf', $tree1);
doesn't work, as it copies only the first root node.
Ok, so then I thought I would traverse the whole first tree, adding every element one by one to the second one.
But consider XML like this:
<root>
aaa
<bbb/>
ccc
</root>
How do I access "ccc"? tree1->children()
returns just "bbb"... .
You can use this class to SimpleXML objects that accept children append
This is nice solution from comment on PHP manual page (using only SimpleXML, not DOM):
There also is sample of usage.
Very nice Theo Heikonnen Slight tweaking to make it work the way I wanted
You can't add a "tree" directly using SimpleXML, as you have seen. However, you can use some DOM methods to do the heavy lifting for you whilst still working on the same underlying XML.