Let’s say I have to xml documents:
<first_root>
<element1/>
<element2>
<embedded_element/>
</element2>
</first_root>
and
<foo>
<bar/>
</foo>
How can I put this second xml doc into the first one, using php and DomDocument or SimpleXML?
I want it to look like something like this:
<first_root>
<element1/>
<element2>
<foo>
<bar/>
</foo>
<embedded_element/>
</element2>
</first_root>
You can do it using DOMDocument:
Here I imported the XML into
DOMDocuments, then I picked up the firstembedded_elementoccurrence andfoonode. After you have to import deeplyfoointo the first document. Now you can insertfoobeforeembedded_element.Obviously this is only the happy case…
Documentation: DOM
With
SimpleXMLyou can accomplish this by building a third document based on the first two because you can’t append SimpleXMLElements into others. (Or maybe you can but there’s something I didn’t get)