0

I am using XPath to query an xml document and then copy found nodes to a separate results xml file, however the problem I am having is that the XML declaration at the top gets added each time a new node is added to the results file.

Here is my php code:

$allEventsForVenue = $xPath->query(
            "/Ticket/EventsPoints/Event[contains(PerformanceName,'$searchParam')]"
    );

    foreach ($allEventsForVenue as $event) {

         $dstDom = new DOMDocument('1.0', 'utf-8');
         $dstDom->appendChild($dstDom->createElement('EventsPoints'));
         $dstDom->documentElement->appendChild($dstDom->importNode($event, true));
         //echo $dstDom->saveXml();
         $dstDom->formatOutput = true;
         $strxml .= $dstDom->saveXML();
         $handle = fopen("/var/www/html/xml/searchresults$searchID.xml", "w");
         fwrite($handle, $strxml);
         fclose($handle); 
    } 

Invalid XML File (two xml declarations in the one file):

->>         <?xml version="1.0" encoding="utf-8"?>
            <EventsPoints>
              <Event ID="17">
                    <PerformanceName>Bressie</PerformanceName>
                    <PresaleTime/>
                    <PriceRange>17 - 17</PriceRange>
                    <VenueID ID="19"/>
                </Event>
            </EventsPoints>
->>         <?xml version="1.0" encoding="utf-8"?>
            <EventsPoints>
              <Event ID="180">
                    <PerformanceName>U2</PerformanceName>
                    <PriceRange>20 - 20</PriceRange>
                    <VenueID ID="198"/>
                </Event>
            </EventsPoints>
4
  • Repeated fopen and fclose in foreach loop will decrease the performance. Commented May 31, 2012 at 9:59
  • thanks but is there any way to only run this line : $dstDom = new DOMDocument('1.0', 'utf-8'); once (on the first loop) Commented May 31, 2012 at 10:02
  • Check my answer. I created DOMDocument only once. and repeatedly append elements in it then wrote it to a file. Commented May 31, 2012 at 10:03
  • XSLT may help you with these kinds of operations, though it's perhaps not as easy to read as PHP :) Commented May 31, 2012 at 23:07

3 Answers 3

3

You are creating new DOMDocument instance in loop. Also you are writing in each iteration. This is making new XML document on each iteration and its get appended in your xml file. What you need to do is, loop the XML generation part. Not the whole thing.

If you loop only the appendChild parts, your problem will be solved.

$dstDom = new DOMDocument('1.0', 'utf-8');
$dstDom->formatOutput = true;    

foreach ($allEventsForVenue as $event) {
     $dstDom->appendChild($dstDom->createElement('EventsPoints'));
     $dstDom->documentElement->appendChild($dstDom->importNode($event, true));
}

file_put_contents("/var/www/html/xml/searchresults$searchID.xml", $dstDom->saveXML());
Sign up to request clarification or add additional context in comments.

Comments

1

DomDocument lets you output a specific node without the xml declaration.

Just include a node in saveXML, like saveXML($node), with node being an XMLNode type object.

With this method I think you can bypass the whole "create sub-xmldocument/import node" thing, by outputting directly the desired node in a string.

1 Comment

Thank you. Remember to check always PHP documentation, one may overlook that some features are really easy to find there: php.net/manual/en/domdocument.savexml.php
0

use something like this

enter <?php
 // Continued from example XML above.

 /* Search for <a><b><c> */
  $result = $xml->xpath('/a/b/c');

  while(list( , $node) = each($result)) {
    echo $node->asXML();
 }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.