How is possible add child to the specific node

前端 未结 3 1478
终归单人心
终归单人心 2021-01-27 05:09

This is my starting xml :

 
    
      
        
     &         


        
相关标签:
3条回答
  • 2021-01-27 05:44

    Some good examples on how to append XML nodes can be found in the PHP manual. Like this one on how to use the SimpleXMLElement class to add a child node.

    0 讨论(0)
  • 2021-01-27 05:47

    With DOMDocument it's as easy as this:

    $child = new DOMElement('children');
    $parent->appendChild($child);
    

    Whereas $parent is the DOMElement parent which (after you have updated your question) to aquire is part of your problem:

    // append <children> to the first <child> element
    $parent = $dom->getElementsByTagName('child')->item(0);
    $child = new DOMElement('children');
    $parent->appendChild($child);
    
    0 讨论(0)
  • 2021-01-27 05:53

    Since you have tagged php i am assuming you are looking for a solution in PHP did you try the following:

    $myxml = new SimpleXMLElement($xmlstr);
    $myxml->child[0]->addChild('children');
    

    See: http://www.php.net/manual/en/simplexml.examples-basic.php for a good set of examples on XML manipulation in PHP

    0 讨论(0)
提交回复
热议问题