PHP and DOMDocument

后端 未结 1 1966
自闭症患者
自闭症患者 2021-01-26 05:02

I have a question regarding the use of the DOMDocument and creating XML.

I have a PHP program that

  1. loads in an XML file
  2. processes each node (r
相关标签:
1条回答
  • 2021-01-26 06:03

    To append XML (as a string) into another element, you create a document fragment which you can append then:

    // create new node
    $subElt = $domDoc->createElement('MyResponse');
    
    // create new fragment
    $fragment = $domDoc->createDocumentFragment();
    $fragment->appendXML($cleanSubNode);
    $subElt->appendChild($fragment);
    

    This will convert the raw XML into domdocument elements, it's making use of the DOMDocumentFragment::appendXML function.

    Edit: Alternatively in your use-case (thx to the comments), you can directly use the simplexml object and import it into your domdocument:

    // create subelement
    $subElt = $domDoc->createElement('MyResponse');
    
    // import simplexml document
    $subElt->appendChild($domDoc->importNode(dom_import_simplexml($resultXMLNode), true));
    
    // We insert the new element as root (child of the document)
    $domDoc->appendChild($subElt);
    

    No need to convert the response into a string and do the replace operation you do with it any longer with this.

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