Is there a way to add a PHP SimpleXMLElement to another SimpleXMLElement?

后端 未结 2 724
孤城傲影
孤城傲影 2020-12-04 00:56

The \"addChild\" method of SimpleXMLElement seems like it should be the right choice, but it apparently only takes strings representing the tagname of the new child.

相关标签:
2条回答
  • 2020-12-04 00:59

    I'm not sure if this will help but you could also extend SimpleXML to work as you expect. Here is a project I worked on for bLibrary Xml class. You can look at it to tailor SimpleXml to work as you expect it.

    0 讨论(0)
  • 2020-12-04 01:20

    Hey unknown. You have to use the DOMElement interface to your SimpleXML objects to achieve this.

    <?php
    
    $s = new SimpleXMLElement('<root/>');
    $t = new DOMElement('child');
    
    $dom = dom_import_simplexml($s);
    $dom->appendChild($t);
    
    echo $s->asXML();
    // <root><child/></root>
    

    If you need more specific details, let me know. There are several examples in the documentation and comments for the dom_import_simplexml() method too: http://php.net/dom_import_simplexml

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