|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Wed, Apr 9, 2008 at 9:35 AM, Christoph Boget
<christoph.boget@gmail.com> 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/functio...importnode.php Andrew |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> 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/functio...importnode.php Interesting. Why is that, out of curiosity? Thanks for the pointer! That's exactly what I needed. ![]() thnx, Chris |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Wed, Apr 9, 2008 at 10:17 AM, Christoph Boget
<christoph.boget@gmail.com> wrote: > > 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/functio...importnode.php > > Interesting. Why is that, out of curiosity? > > Thanks for the pointer! That's exactly what I needed. ![]() > > thnx, > Chris > Well, it's part of the DOM specification. I'm not totally sure why (and don't have the inclination to read all about it), but DOM nodes are specific to the document and contain pointers and other information related to that document. If you really want to know moe details, you can check out the W3C site (or STW). http://www.w3.org/DOM/faq.html#moveNode Andrew |
|
![]() |
| Outils de la discussion | |
|
|