2013-04-05 2 views
0

J'ai une simple chaîne XML:PHP/SimpleXML. Comment ajouter un enfant au noeud renvoyé par xpath?

$sample = new SimpleXMLElement('<root><parent><child1></child1></parent></root>'); 

et j'essaie de trouver le noeud avec XPath() et ajoutez l'enfant à ce noeud.

$node = $sample->xpath('//parent'); 
$node[0]->addChild('child2'); 
echo $sample->asXML(); 

Comme vous le voyez child2 est ajouté en tant qu'enfant de child1, non pas comme un enfant de parent. Mais si je change mon XML, addChild() fonctionne très bien.

<root> 
    <parent> 
    <child1> 
     <child2></child2> 
    </child1> 
    </parent> 
</root> 

Ce code

$sample = new SimpleXMLElement('<root><parent><child1><foobar></foobar></child1></parent></root>'); 
$node = $sample->xpath('//parent'); 
$node[0]->addChild('child2'); 
echo $sample->asXML(); 

retours

<root> 
    <parent> 
    <child1> 
     <foobar></foobar> 
    </child1> 
    <child2> 
    </child2> 
    </parent> 
</root> 

J'ai donc deux questions:

  1. Pourquoi?
  2. Comment puis-je ajouter child2 en tant qu'enfant de parent, si child1 n'a pas d'enfant? Xpath() renvoie les ENFANTS de l'élément qui lui a été transmis.
+1

Quelle version de PHP et libxml2 utilisez-vous? Votre code "cassé" [fonctionne pour moi] (http://3v4l.org/JiGAf#v513). – salathe

+0

2.7.8 et 5.4 = ( –

+1

Dans ce cas, le lien que j'ai donné montre que votre code fonctionne bien pour 2.7.8 sur 5.4.0 – salathe

Répondre

0

Ainsi, lorsque vous ajoutez addChild() au premier élément xpath(), vous ajoutez un enfant au premier élément de parent, qui est child1. Lorsque vous exécutez ce code, yo uwill voir qui est considérée comme la création d'un élément « ParentChild » comme un enfant de « parent » -

<?php 
$original = new SimpleXMLElement('<root><parent><child1></child1></parent></root>'); 
$root = new SimpleXMLElement('<root><parent><child1></child1></parent></root>'); 
$parent = new SimpleXMLElement('<root><parent><child1></child1></parent></root>'); 
$child1 = new SimpleXMLElement('<root><parent><child1></child1></parent></root>'); 
$tXml = $original->asXML(); 
printf("tXML=[%s]\n",$tXml); 
$rootChild = $root->xpath('//root'); 
$rootChild[0]->addChild('rootChild'); 
$tXml = $root->asXML(); 
printf("node[0]=[%s] tXML=[%s]\n",$rootChild[0],$tXml); 
$parentChild = $parent->xpath('//parent'); 
$parentChild[0]->addChild('parentChild'); 
$tXml = $parent->asXML(); 
printf("node[0]=[%s] tXML=[%s]\n",$parentChild[0],$tXml); 
$child1Child = $child1->xpath('//child1'); 
$child1Child[0]->addChild('child1Child'); 
$tXml = $child1->asXML(); 
printf("node[0]=[%s] tXML=[%s]\n",$child1Child[0],$tXml); 
?> 

tXML=[<?xml version="1.0"?> 
<root><parent><child1/></parent></root>] 
tXML=[<?xml version="1.0"?> 
<root><parent><child1/></parent><rootChild/></root>] 
tXML=[<?xml version="1.0"?> 
<root><parent><child1/><parentChild/></parent></root>] 
tXML=[<?xml version="1.0"?> 
<root><parent><child1><child1Child/></child1></parent></root>] 
+0

Vérifiez simplement votre 'parentChild En fait, ce n'est pas l'enfant de 'parent', son enfant de' child1'. –

Questions connexes