PHP/SimpleXML。如何将子节点添加到xpath返回的节点?

PHP/SimpleXML。如何将子节点添加到xpath返回的节点?,php,xpath,simplexml,addchild,Php,Xpath,Simplexml,Addchild,我有一个简单的XML字符串: $sample = new SimpleXMLElement('<root><parent><child1></child1></parent></root>'); 如您所见,child2是作为child1的子项添加的,而不是作为父项的子项添加的 <root> <parent> <child1> <child2></

我有一个简单的XML字符串:

$sample = new SimpleXMLElement('<root><parent><child1></child1></parent></root>');
如您所见,
child2
是作为
child1
的子项添加的,而不是作为父项的子项添加的

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

但是,如果我更改XML,addChild()工作得很好。此代码

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

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

所以我有两个问题:

  • 为什么?
  • 如果
    child1
    没有子级,如何将
    child2
    添加为
    parent
    的子级
  • xpath()返回传递给它的元素的子元素。因此,当您将child()添加到xpath()返回的第一个元素时,实际上是在将一个子元素添加到parent的第一个元素,即child1。当您运行这段代码时,您将看到它正在创建一个“parentChild”元素作为“parent”的子元素-

    
    tXML=[
    ]
    tXML=[
    ]
    tXML=[
    ]
    tXML=[
    ]
    
    只需检查您的
    父母子女
    。实际上它不是
    parent
    的子对象,而是
    child1
    的子对象。您使用的是什么版本的PHP和libxml2?你的“坏”代码。在这种情况下,我给出的链接显示你的代码在5.4.0版本的2.7.8版本中运行良好。我只能强调萨拉特已经编写的内容:你问题中的代码确实有效。我也无法想象为什么会发生在你身上。您可能希望在添加子项之前先进行调试,例如:
    var_dump($node[0]->asXML())
    <root>
      <parent>
        <child1>
          <foobar></foobar>
        </child1>
        <child2>
        </child2>
      </parent>
    </root>
    
    <?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>]