0

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

Rob
  • 173
  • 1
  • 5
  • 15
  • Make up your mind on which library you want for processing XML in PHP. You are using SimpleXML and PHPDOM at the same time and I don't think they work well with each other. – timdream Nov 05 '11 at 07:07
  • Are you referring to my initial simplexml_load_file()? If so, not sure this affects my final output since that's just a driver file. thanks for your comment. – Rob Nov 05 '11 at 07:29
  • I changed the line $subNode = $addressXML->asXML(); to use saveXML() with the same result :( – Rob Nov 05 '11 at 07:41

1 Answers1

5

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    or deep import the node and append it – Gordon Nov 05 '11 at 09:07
  • 1
    I think he's referring to importnode: http://php.net/manual/en/domdocument.importnode.php – CD001 Nov 05 '11 at 09:14
  • Added that to the answer now as well, that's really good to know. – hakre Nov 05 '11 at 09:32
  • upvote for showing that simplexml and PHP DOM does work together. – timdream Nov 05 '11 at 17:17
  • I tried both variations of Hakre's code and both were wonderfully successful! I then I went a little deeper (simpler). It turned out that the $resultXMLNode I was getting was already properly formatted - i.e. I did not not to wrap this in another element ('MyResponse'). – Rob Nov 06 '11 at 07:27
  • Continuing... So my final code was this: $resultXMLNode = $myAddress->buildRequest() ; $rootNode->appendChild($domDoc->importNode(dom_import_simplexml($resultXMLNode), true)); – Rob Nov 06 '11 at 07:39