Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:06:36+00:00 2026-06-04T18:06:36+00:00

I am using XPath to query an xml document and then copy found nodes

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:06:38+00:00Added an answer on June 4, 2026 at 6:06 pm

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to select specific values from a xml document using XPath. The
I am trying to select specific values from a xml document using XPath. The
With the following XML: <parent> <child>Stuff</child> <child>Stuff</child> </parent> Using XPath I query the child
I have a simple XML and i signed the XML using XPATH query like
I'm using GData as my XML parser. I've tried using an XPath query to
I'm using XPath in .NET to parse an XML document, along the lines of:
I have some XML and an XPath query. I'm using Yahoo! widgets, so I'm
I want to do an XPath query on this file (excerpt shown): <?xml version=1.0
I am using the following code to query some XML with XPath I get
My goal is executing an XQuery using XPath. My XML file is: <?xml version=1.0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.