Re: [PHP] Re: SimpleXML & libxml options (XInclude)



Ben Roberts wrote:
Thanks Rob. That works. And it appears to work flawlessly if you subsequently convert it back to a SimpleXML object too:

$xml = new SimpleXMLElement($xml_file, 0, true);

$dom = dom_import_simplexml($xml);
$dom->ownerDocument->xinclude();

$xml = simplexml_import_dom($dom);
Dump($xml);
No need to even do that. The document is the same document (zero copy which is why going back and forth is efficient), so when its changed by the xinclude call, it is reflected in $xml already. Only reason you would need to do conversions back and forth is if you need specific nodes that might have been added/changed by the other extension. Here you are using the same root element and dont have to worry about losing any child references you might already have, so its a wasted call.
Is it likely that this will be supported directly by SimpleXML in future versions of PHP?
Unless the libxml2 parser in SimpleXML can take advantage of the option, an XInclude method is not going to be added to SimpleXML. For things like that, and full XPath support, etc.., you need to interop with DOM so SimpleXML can be kept simple.

Rob
.



Relevant Pages

  • Re: reading a XML file on every page execution - convert to PHP?
    ... into a simpleXML node every time index.php loads. ... Including a php file requires the file be read then parsed. ... modification date- and use the .xml only if it was newer. ... cannot load, ...
    (comp.lang.php)
  • Re: How to open and read "feed:" in PHP?
    ... Question: Is a URL that starts with feed: output XML? ... Question: How do I open a feed: URL in PHP and parse it? ... I looked at the example for simpleXML on the php.net web site. ... That's because SimpleXML alows you to navigate the xml tree as you wish, foroward and backwards how many times you like. ...
    (comp.lang.php)
  • Re: Interaction between SimpleXML & DOM
    ... The above is not well-formed XML. ... Give up using SimpleXML and use DOM for everything... ... $contentDOM = new DOMDocument; ...
    (comp.lang.php)
  • Re: How to open and read "feed:" in PHP?
    ... Question: Is a URL that starts with feed: output XML? ... Question: How do I open a feed: URL in PHP and parse it? ... Now you have a SimpleXML object named $rss which you can use to iterate. ... SimpleXML alows you to navigate the xml tree as you wish, ...
    (comp.lang.php)