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



Ben Roberts wrote:
So really when you perform:

$dom = dom_import_simplexml($xml);

the $dom object is really created by taking a reference to the $xml object rather than copying it? i.e. (I know this isn't real code)

$dom = dom_import_simplexml(& $xml);
Not exactly, but the idea is along those lines.
What is happening is that $dom (DOMElement in this case) now points to the same libxml2 structure as does $xml (SimpleXMLElement).
So now when you modify the structure of the document with one, it is reflected by the other because they are in the same document. This also means that it is possible that objects can become invalid by changes made in the other extension.

$sxe = new SimpleXMLElement("<root><child><inner>text</inner></child></root>");
$sxechild = $sxe->child->inner[0];

$dom = dom_import_simplexml($sxe);
$dom->removeChild($dom->firstChild);

var_dump($sxechild);

PHP Warning: var_dump(): Node no longer exists in /home/rrichards/temp.php on line 8
object(SimpleXMLElement)#3 (0) {
}
This shows that $sxechild is still an SimpleXMLElement, but warnings are issues that the node it refers to in the document no longer exists.
Although I doubt someone use the code above, it is the smallest piece of code I could write to demonstrate the possibility.

Rob
.



Relevant Pages

  • Re: XmlTextreader versus DOM
    ... I've heard them referred to as "DOM" and "SAX". ... DOM is, generally speaking, the easiest way in which to deal with XML ... "SAX" (named after the original parser, I think) parsers read one XML ...
    (microsoft.public.dotnet.languages.vc)
  • Re: XmlTextreader versus DOM
    ... I've heard them referred to as "DOM" and "SAX". ... DOM is, generally speaking, the easiest way in which to deal with XML ... "SAX" (named after the original parser, I think) parsers read one XML ...
    (microsoft.public.dotnet.xml)
  • XML parse/build metaphor mismatch
    ... XML data in arbitrary order that I'm simply missing. ... It seems to be easy getting stuff -out- of a DOM via XPath, ...
    (comp.text.xml)
  • Re: javascript and XML help
    ... Some Text and html tags ... That is not well-formed XML so any XML parser will give a parse error. ... DOM nodes, it does not help that some of them might have the same tag ...
    (comp.lang.javascript)
  • Re: Java with xml
    ... There are three main XML parsing techniques: ... - Document Object Model (DOM), ... Simple API for XML (SAX), ... built in memory. ...
    (comp.lang.java.programmer)