Hi All,
I am moving some code from SimpleXML to php's DOM and don't quite get the objects yet.
In SimpleXML, for example, I can create nested elements very easily like so:
$Settings = new SimpleXMLElement("<root></root>");
$Settings->addChild('el1');
$Settings->el1->addChild('el2');
$Settings->el1->el2->addChild('el3');
$Settings->el1->el2->el3->addChild('el4', 'text');
echo("<pre>".htmlspecialchars(str_replace("><", ">\n<",$Settings->asXML()))."</pre>");
// Result
<?xml version="1.0"?>
<root>
<el1>
<el2>
<el3>
<el4>text</el4>
</el3>
</el2>
</el1>
</root>
in DOM though, it seems like I need to either keep track of the handles, or fetch a list and loop through it in order to get a DOMElement for a particular element. There must be an easier way that I am missing...
Say I have the below code, and want to add el1, el2, el3, etc. like above. What is the least verbose way to do that? Preferably without having to have a variable as a handle for each node I want to add a child to, or having to fetch them via loops.
$DOMXML = new DOMDocument();
$DOMXML->appendChild($DOMXML->createElement('root'));
$DOMXML->documentElement->appendChild($DOMXML->createElement('el1));
// ...
echo($DOMXML->saveXML(););
Thanks!