tags:

views:

314

answers:

1

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!

A: 

Hi,

I don't think there is a way to do what you want using DOM :-(
I tried a while ago, when I had to generate some XML files with DOM...

The only thing I could come up with was something like what you are probably refering to ; a long long bunch of code ; like this, I suppose :

$DOMXML = new DOMDocument();

$root = $DOMXML->createElement('root');
$DOMXML->appendChild($root);

$elt1 = $DOMXML->createElement('el1');
$root->appendChild($elt1);

$elt2 = $DOMXML->createElement('el2');
$elt1->appendChild($elt2);

$elt3 = $DOMXML->createElement('el3');
$elt2->appendChild($elt3);

$elt4 = $DOMXML->createElement('el4');
$elt4->appendChild($DOMXML->createTextNode('text'));
$elt3->appendChild($elt4);

echo("<pre>".htmlspecialchars(str_replace("><", ">\n<",$DOMXML->saveXML()))."</pre>");

That is indeed quite long :-(
OK, it's powerful, you can do lots of things with it... But, still, a pain to write ^^


An alternative solution would be to use both the combined powers of SimpleXML and DOM :

First, create (simply) your XML Document using SimpleXML, like you said earlier :

$simple2 = new SimpleXMLElement("<root></root>");
$simple2->addChild('el1');
$simple2->el1->addChild('el2');
$simple2->el1->el2->addChild('el3');
$simple2->el1->el2->el3->addChild('el4', 'text');

And, then, convert it to DOMDocument, using dom_import_simplexml :

// Convert to DOMDocument
$dom2 = new DOMDocument();
$dom2->appendChild(
    $dom2->importNode(
        dom_import_simplexml($simple2), 
        true
    )
);

// Now, you can do your stuff that requires a DOMDocument object ;-)
// Well, right now, displaying it will be enough, I suppose ?

echo("<pre>".htmlspecialchars(str_replace("><", ">\n<",$dom2->saveXML()))."</pre>");

You'll get exactly the same ouput :

<?xml version="1.0"?>
<root>
<el1>
<el2>
<el3>
<el4>text</el4>
</el3>
</el2>
</el1>
</root>

It might not be the simplest solution, nor the most efficient one... But it seems to be working, and doing the job ;-)

If anyone has another idea... I'm interested too, for the next time I'll have to deal with DOMDocument !

Pascal MARTIN
Thanks! Maybe I can keep all the handles in an array of some sort to avoid having dozens of variables. Wish I could just use simplexml (I was originally), but I am working with an object that just hates it. I don't know why, but this chart refuses to function with anything that has ANYTHING to do with SimpleXML. http://www.amcharts.com/forum/viewtopic.php?pid=9418#p9418
Eli