Re: Working with XML: DomDocument or SimpleXML?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux