[PHP] Re: Working with XML: DomDocument or SimpleXML?
Colin Guthrie wrote: snip / I know I definitely want to do XSLT stuff and for that I need to use DomDocument (I'm sure there are other ways but this works fine for me so far!). Fairly unknown tidbit: you can pass SimpleXML objects to the XSL extension. XSL will use the document from it. snip / I'll phrase it better: one can easily convert a SimpleXML object to a DomDocument[1], but nothing is free (in terms of time taken and memory requirements etc.), so really I guess I want to ask if the trade off of the simplicity of working with SimpleXML is worth it considering the overhead of the conversion process to a DomDocument for subsequent XSLT transforms etc? Conversion is easy and the time/memory is negligible due to how the interoperability was designed. The whole purpose of it is so you can go back and forth to use whichever API suits you need at the time. if ($xml instanceof SimpleXMLElement) { $rv = new DOMDocument('1.0', 'utf-8'); $node = dom_import_simplexml($xml); $node = $rv-importNode($node, true); $rv-appendChild($node); return $rv; } Why??? If you really want a DOMDocument object why are you creating a new document, copying nodes and incurring all the additional overhead? if ($xml instanceof SimpleXMLElement) { /* No copying, just use the existing XML tree directly */ $node = dom_import_simplexml($xml); return $node-ownerDocument; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Working with XML: DomDocument or SimpleXML?
Rob wrote: Fairly unknown tidbit: you can pass SimpleXML objects to the XSL extension. XSL will use the document from it. Nice to know... /me will have to experiment! if ($xml instanceof SimpleXMLElement) { $rv = new DOMDocument('1.0', 'utf-8'); $node = dom_import_simplexml($xml); $node = $rv-importNode($node, true); $rv-appendChild($node); return $rv; } Why??? If you really want a DOMDocument object why are you creating a new document, copying nodes and incurring all the additional overhead? if ($xml instanceof SimpleXMLElement) { /* No copying, just use the existing XML tree directly */ $node = dom_import_simplexml($xml); return $node-ownerDocument; } Yeah I had that initially too but that did not produce a DOMDocument Object but a DOMNode Object (or something like that will have to test again as memory is corrupted - there /was/ a reason, and stupid me didn't comment the code.). Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Working with XML: DomDocument or SimpleXML?
Rob wrote: It had to have been for some other reason as the return type for the above example is a DOMDocument. A raw DOMNode is *NEVER* returned from the DOM extension. It is simply a base class for most of the DOM classes, such as DOMDocument. I think I just didn't do the -ownerDocument or something daft like that in the past. This is great (still need to test but I trust you!) as it means we can work happily with either model quite happily (for two reasons if you can just pass the SimpleXML object straight into the XSLT stuff!!) Cheers Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Working with XML: DomDocument or SimpleXML?
Colin Guthrie wrote: Rob wrote: if ($xml instanceof SimpleXMLElement) { /* No copying, just use the existing XML tree directly */ $node = dom_import_simplexml($xml); return $node-ownerDocument; } Yeah I had that initially too but that did not produce a DOMDocument Object but a DOMNode Object (or something like that will have to test again as memory is corrupted - there /was/ a reason, and stupid me didn't comment the code.). It had to have been for some other reason as the return type for the above example is a DOMDocument. A raw DOMNode is *NEVER* returned from the DOM extension. It is simply a base class for most of the DOM classes, such as DOMDocument. Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Working with XML: DomDocument or SimpleXML?
Robert Cummings wrote: I still use PHP4 so I wrote my own XML handling class that wraps the xml_xxx() series of functions. Haven't had a problem with it. Makes working with XML very easy since it uses a path string syntax to focus/access nodes and attributes: Cheers for that. I know I definitely want to do XSLT stuff and for that I need to use DomDocument (I'm sure there are other ways but this works fine for me so far!). I guess think I asked too open a question, when really I'm looking for fairly specific answers (or rather opinions). I'll phrase it better: one can easily convert a SimpleXML object to a DomDocument[1], but nothing is free (in terms of time taken and memory requirements etc.), so really I guess I want to ask if the trade off of the simplicity of working with SimpleXML is worth it considering the overhead of the conversion process to a DomDocument for subsequent XSLT transforms etc? Col [1] e.g. if ($xml instanceof SimpleXMLElement) { $rv = new DOMDocument('1.0', 'utf-8'); $node = dom_import_simplexml($xml); $node = $rv-importNode($node, true); $rv-appendChild($node); return $rv; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php