I have a question regarding the use of the DOMDocument and creating XML.
I have a PHP program that
- loads in an XML file
- processes each node (row) of XML; sends it off to another process which then returns an XML element
- 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 < >, 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