The "addChild" method of SimpleXMLElement seems like it should be the right choice, but it apparently only takes strings representing the tagname of the new child.
There's the object-ish notation for referencing nodes of the tree and setting them, e.g. $simpleXMLNode->child = value, but that only seems to work for simple text/numeric values. If I try the following:
$s = new SimpleXMLElement('<root/>');
$t = new SimpleXMLElement('<child/>');
$s->a = $t;
echo $s->asXML()
I get:
<?xml version="1.0"?>
<root><a></a></root>
when I was hoping for:
<?xml version="1.0"?>
<root><a><child/></a></root>
I thought of converting $t to a string and then adding it (after stripping out the XML declaration):
$s->a = substr($t->asXML(),22)
But this yields:
<?xml version="1.0"?>
<root><a><child/></a></root>
Again, not what I was hoping for.
Is there a typical way to accomplish this kind of thing with SimpleXML?
Hey unknown. You have to use the DOMElement interface to your SimpleXML objects to achieve this.
If you need more specific details, let me know. There are several examples in the documentation and comments for the dom_import_simplexml() method too: http://php.net/dom_import_simplexml