Re: Problem with DOMElement/Node

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

 



On Wed, Apr 9, 2008 at 9:35 AM, Christoph Boget
<christoph.boget@xxxxxxxxx> wrote:
> Could someone explain to me what I'm doing wrong?
>
>  I'm trying to get an element from one DOMDocument and append it to a
>  different DOMDocument.  The (simplified) output of saveXML() from the
>  first DOMDocument is as follows:
>
>  <?xml version="1.0" encoding="UTF-8"?> <BranchRoot
>  id="root"><option>1</option><option>2</option></BranchRoot>
>
>  Here is a snippet of code:
>
>  <?php
>
>  $oXmlDocument = new DOMDocument( '1.0', 'UTF-8' );
>  $oRootNode    = $oXmlDocument->createElement( 'menu' );
>  $oRootNode->setAttribute( 'id', 'root' );
>  $oRootNode->setIdAttribute( 'id', TRUE );
>
>  $oRootNode->setAttribute( 'style', $sStyle );
>  $oRootNode->setAttribute( 'width', $iWidth );
>  $oRootNode->setAttribute( 'target', $sTarget );
>  $oRootNode->setAttribute( 'indent', $iIndent );
>
>  $oXmlDocument->appendChild( $oRootNode );
>
>  $oNewChildEl = $oFirstDoc->getElementById( 'root' );
>  $oRootNode->appendChild( $oNewChildEl );
>  ?>
>
>  I'm printing out what $oNewChildEl is to see if it's not returning the
>  proper element, using
>
>  echo '[' . $oNewChildEl->tagName . ']' . var_dump( $oNewChildEl );
>
>  and I'm seeing:
>
>  object(DOMElement)#1055 (0) { } [BranchRoot]
>
>  so it does look like it's returning the proper DOMElement.  But even
>  so, I'm getting a fatal error when $oRootNode is trying to
>  appendChild().  Specifically, the error I'm getting is
>
>  Fatal error: Uncaught exception 'DOMException' with message 'Wrong
>  Document Error'
>
>  What's going on?  It doesn't seem like I'm doing anything wrong but
>  something is causing the problem and I apparently do not understand
>  exactly what.
>
>  Could anyone lend any insight as to what's going on?  And what I might
>  do to get what I need done?
>
>  thnx,
>  Chris

DOM Nodes are specific to the document in which they were created, so
you can't just append a node from one document into another document.
The importNode function does what you want.

http://us2.php.net/manual/en/function.dom-domdocument-importnode.php

Andrew

-- 
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