PHP5: DOMDocument loadXML bug
[email protected] (Vivian Steller) Thu, 11 Nov 2004 22:17:44 +0100
| Newsgroups | php.xml.dev |
|---|---|
| Organization | eecoo |
| Message-ID | <[email protected]> |
good evening at all,
i found out some strange behaviour with the DOMDocument::loadXML method (of
php5.0.3dev build from oct 31, 2004, hadn't the time to check some new
version, sorry).
to put it in a nutshell the problem concerns extending
DOMDocument/DOMElement and overloading the loadXML method, maybe other
methods, too.
please have a look at the following code and the comments document the
output:
######################### CODE START #########################
<pre>
<?php
class MyDOMElement extends DOMElement {
public function test() {
print("ok");
}
}
class MyDOMDocument extends DOMDocument {
public function loadXML($xml) {
parent::loadXML($xml);
$newRoot = new MyDOMElement("newroot", "value");
print $newRoot->test() . "\n";
$newRoot = $this->importNode($newRoot);
// we want to change the current root element to another one...
$this->replaceChild($newRoot, $this->documentElement);
print $this->documentElement->nodeName . "\n";
// returns "newroot", worked fine so far, but...
print get_class($this->documentElement) . "\n";
// returns "DOMElement", thus seems to me importNode($node) makes a
$this->createElement($node->nodeName, $node->nodeValue) internally?
// ok, if this behaviour couldn't be changed, we maybe could include some
little work-around and write our own importNode func...
// BUT EVEN THAT WOULDN'T WORK, because ...
$anotherRoot = $this->createElement("anotherRoot");
// now we used the doc itself to create the node and consequently we do
not need to import the node...
$this->removeChild($this->documentElement); // we go the really basic way
$this->appendChild($anotherRoot);
print get_class($this->documentElement) . "\n";
// returns "MyDOMClass" quite right, root is now a MyDOMElement,
// but see the comments below...
}
public function createElement($name, $value = "") {
return new MyDOMElement($name, $value);
}
}
$doc = new MyDOMDocument();
$doc->loadXML("<root/>");
print(htmlspecialchars($doc->saveXML()));
// here the result is right: ".. <anotherElement/>", but ...
// print_r($doc->documentElement->test());
// throws a "Fatal error: Call to undefined method DOMElement::test()
// in .../domdocument.php on line xx" <- the commented line above (47)...
// and if you
print get_class($doc->documentElement) . "\n";
// the result is again "DOMElement"?!
// strange behaviour, i think?!
?>
</pre>
####################### CODE END ##########################
as you can see, inside the loadxml method, everything (disregarding
importNode) works fine. but leaving the method, php seems to do something
"magic" in the background, isn't it?
If it is the case, would there be a possibility to fix this bug untill one
of the next releases? that would really be great...
sorry if i post (possibly) bugs here, but i'm not familiar with the bug
system yet and this is the fastest way to get through to the developers:)
thanks in advance...
vivian