I have 2 XML files.
I am trying to get data from XML File B and add it to XML File A. I want to first add an empty child to XML File A, and then in that child, I want to add data from XML File B (in this case, the animal
child element). I want to achieve this through either SimpleXML or DOMDocument. I have been stuck on this. Any help would be great!
personinfo.xml < XML File A
<personinfo>
<personN number="1">
<personL letter="A">
<fullname>
<firstname>Summer</firstname>
<lastname>Smith</lastname>
</fullname>
<favourites>
<color>pink</color>
</favourites>
</personL>
</personN>
<personN number="2">
<personL letter="B">
<fullname>
<firstname>Autumn</firstname>
<lastname>Smith</lastname>
</fullname>
<favourites>
<color>blue</color>
</favourites>
</personL>
</personN>
</personinfo>
favouritesinfo.xml < XML File B
<favouritesinfo>
<personN number="1">
<animal>cat</animal>
</personN>
<personN number="2">
<animal>dog</animal>
</personN>
</favouritesinfo>
Is this possible? If so, am I able to do this via XML DOM Document or SimpleXML?
I attempted doing it this way via a separate PHP file but it does not work/do anything with SimpleXML
$personXML = simplexml_load_file("personinfo.xml");
$faveXML = simplexml_load_file("favouritesinfo.xml");
$animal=$faveXML->personN[0]->animal;
$newFave=$personXML->personN->personL[0]->addChild("favourites");
$favourites->addChild($animal);
$personXML->asXML();
Desired output
<personinfo>
<personN number="1">
<personL letter="A">
<fullname>
<firstname>Summer</firstname>
<lastname>Smith</lastname>
</fullname>
<favourites>
<color>pink</color>
</favourites>
<favourites>
<color>cat</color>
</favourites>
</personL>
</personN>
<personN number="2">
<personL letter="B">
<fullname>
<firstname>Autumn</firstname>
<lastname>Smith</lastname>
</fullname>
<favourites>
<color>blue</color>
</favourites>
<favourites>
<color>dog</color>
</favourites>
</personL>
</personN>
</personinfo>