Re: [PHP-XML-DEV] DOM appendChild question

[email protected] (Christian Stocker) Thu, 26 Feb 2004 09:03:11 +0100
Newsgroups php.xml.dev
Message-ID <[email protected]>

On 2/25/04 8:16 PM, Dan Phiffer wrote:
> Sorry if this is the wrong forum for this, but should this work?

No. The Exception is there for a reason ;)

> 
> $doc = new DomDocument();
> $doc->loadXML('<html><body></body></html>');
> 
> $hello = new DomDocument();
> $hello->loadXML('<p>Hello world!</p>');
> 
> $xp = new DomXPath($doc);
> $body = $xp->query('/html/body')->item(0);
> $body->appendChild($hello->firstChild); // <-- Exception thrown here
> 
> echo $doc->saveHTML();
> 
> Here's the error message I'm getting:
> 
> Fatal error: Uncaught exception 'domexception' with message 'Wrong 
> Document Error' in ... Stack trace: #0 {main} thrown in ... on line 18

Use $doc->importNode($hello->firstChild); before you do the appendChild.

See also 
http://www.w3.org/TR/2004/PR-DOM-Level-3-Core-20040205/core.html#Core-Document-importNode

chregu


> I'm running PHP 5 beta 4 on Apache 1.3.29.
> 
> Thanks!
> -Dan
> 

-- 
christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich
phone +41 1 240 56 70 | mobile +41 76 561 88 60  | fax +41 1 240 56 71
http://www.bitflux.ch  |  [email protected]  |  gnupg-keyid 0x5CE1DECB