Is it possible to insert a PHP SimpleXMLElement
as a child of another Element
$xml = new SimpleXMLElement('<root/>');
$xml_a = new SimpleXMLElement('<parent/>');
$xml_b = new SimpleXMLElement('<child/>');
$xml_b->addAttribute('attribute','value');
$xml_b->addChild('item','itemValue');
// the part will cause and error
$xml_a->addChild($xml_b);
$xml->addChild($xml_a);
I know the above code doesn't work, but that would be the sort of thing I'm looking for.
So that the structure looks like:
<root>
<partent>
<child attribute="value">
<item>itemValue</item>
</child>
</parent>
</root>
I have tried using something like the below to addChild()
:
$dom = dom_import_simplexml($xml_a);
$_xml_ = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
$xml->addChild('parent',$_xml_);
The solution im sure is relatively simple, but I've not used SimpleXMLElement
like this before.