PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Problem with DOMElement/Node
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with DOMElement/Node

Réponse
 
LinkBack Outils de la discussion
Vieux 09/04/2008, 14h35   #1
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with DOMElement/Node

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
  Réponse avec citation
Vieux 09/04/2008, 15h03   #2
Andrew Ballard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Problem with DOMElement/Node

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
  Réponse avec citation
Vieux 09/04/2008, 15h17   #3
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Problem with DOMElement/Node

> 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
  Réponse avec citation
Vieux 09/04/2008, 15h23   #4
Andrew Ballard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Problem with DOMElement/Node

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h47.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11687 seconds with 12 queries