I have a question regarding the use of the DOMDocument and creating XML.
I have a PHP program that
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.