PHP and DOMDocument

2020-05-09 16:54发布

问题:

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 (row) of XML; sends it off to another process which then returns an XML element
  3. I get the string representation of the node so that I can create (append) to a new, resultant XML tree for return to the client

The problem I have is, the XML looks fine until I try and return the final doc back to client. I am using saveXML() and the resultant file contains &lt &gt, etc. When I try to do a save to file [save()] I also get those results. Been searching the PHP boards for hours on this.

Here's my code:

<?php

header('Content-type: text/xml'); 

// **** Load XML ****   
$xml = simplexml_load_file('Test1.xml');     

// Instantiate class; work with single instance
$myAddress = new myAddressClass;        

$domDoc = new DOMDocument('1.0', 'UTF-8');
$domDoc->formatOutput = true;
$rootElt = $domDoc->createElement('root');
$rootNode = $domDoc->appendChild($rootElt);

//Go through each row of XML and process each address
foreach($xml->Row as $row)
{
    //fire off function against instance

    // returns SimpleXMLElement
    $resultXMLNode = $myAddress->buildRequest() ;     

    // need XML representation of node
    $subNode = $addressXML->asXML();                

    // strip out extraneous XML def
    $cleanSubNode = str_replace('<?xml version="1.0"?>', '', $subNode);     

    // create new node
    $subElt = $domDoc->createElement('MyResponse', $cleanSubNode );               

    //append subElmt node
    $rootNode->appendChild($subElt);                                        

}
// need full XML doc properly formatted/valid
$domDoc->saveXML();             

?>

BTW, I am returning the XML to the client so that I can then produce HTML via jQuery.

Any help would be appreciated.

Also, if anyone can offer up a potentially more efficient way of doing this, that'd be great too :)

Thanks.

Rob

回答1:

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.