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 7990535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:02:40+00:00 2026-06-04T13:02:40+00:00

I am using PHP curl to retrive an xml file from a remote url

  • 0

I am using PHP curl to retrive an xml file from a remote url and save it to a local file on my server. The structure is the following:

<Store Country="Ireland">
<EventsPoints>
    <Event ID="1800" >
        <ArtistIDs>
            <ArtistID ID="109" Type="Primary" />
        </ArtistIDs>
        <CategoryID>1</CategoryID>
        <Country>IRL</Country>
        <PerformanceName>Music and Arts</PerformanceName>
        <VenueID ID="197" />
    </Event>
<Venues>
    <Venue ID="197">
        <City>Dublin</City>
        <Country>IRL</Country>
        <VenueName>ABC</VenueName>
        <VenueNumber>22</VenueNumber>
    </Venue>
</Venues>

The above xml blocks are stored in the same XML file. There are several Event blocks and several Venue blocks.
The problem i’m having is using PHP to access this large XML file and iterate through the Venue blocks retrieving only a Venue block with a certain ID specified via a parameter.
I then want to iterate through the event blocks – only retrieving the events matching this specified venue ID. I then want to save this to a file on the server.
I want to do this for each venue.

How do I go about doing the above?

EDIT:

For each Venue and events related to that venue, I just want to literally copy them to their own file – basically splitting down the larger file into individual files

 $docSource = new DOMDocument();
$docSource->loadXML($xml);
$docDest = new DOMDocument();
$docDest->loadXML(file_get_contents('/var/www/html/xml/testfile.xml'));

$xpath = new DOMXPath($docSource);
$id = "197";
$result = $xpath->query('//Event/VenueID[@ID=$id]')->item(0); //Get directly the node you want

$result = $docDest->importNode($result, true); //Copy the node to the other document

$items = $docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result); //Add the copied node to the destination document

echo $docDest->saveXML();
  • 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-04T13:02:41+00:00Added an answer on June 4, 2026 at 1:02 pm

    You are not showing the desired output format, so I will assume this generates what you want. If not, feel free to modify the code so it meets the desired output format. This along with the comments above should have all you need to get this working on your own.

    // load Source document
    $srcDom = new DOMDocument;
    $srcDom->load('/var/www/html/xml/testfile.xml');
    $xPath = new DOMXPath($srcDom);
    // iterate over all the venues in the source document
    foreach ($srcDom->getElementsByTagName('Venue') as $venue) {
        // create a new destination document for the current venue
        $dstDom = new DOMDocument('1.0', 'utf-8');
        // add an EventsPoint element as the root node
        $dstDom->appendChild($dstDom->createElement('EventsPoint'));
        // import the Venue element tree to the new destination document
        $dstDom->documentElement->appendChild($dstDom->importNode($venue, true));
        // fetch all the events for the current venue from the source document
        $allEventsForVenue = $xPath->query(
            sprintf(
                '/Store/EventsPoints/Event[VenueID/@ID=%d]',
                $venue->getAttribute('ID')
            )
        );
        // iterate all the events found in Xpath query
        foreach ($allEventsForVenue as $event) {
            // add event element tree to current destination document
            $dstDom->documentElement->appendChild($dstDom->importNode($event, true));
        }
        // make output prettier
        $dstDom->formatOutput = true;
        // save XML to file named after venue ID
        $dstDom->save(sprintf('/path/to/%d.xml', $venue->getAttribute('ID')));
    }
    

    This will create an XML file like this

    <?xml version="1.0" encoding="utf-8"?>
    <EventsPoint>
      <Venue ID="197">
                    <City>Dublin</City>
                    <Country>IRL</Country>
                    <VenueName>ABC</VenueName>
                    <VenueNumber>22</VenueNumber>
                </Venue>
      <Event ID="1800">
                <ArtistIDs>
                    <ArtistID ID="109" Type="Primary"/>
                </ArtistIDs>
                <CategoryID>1</CategoryID>
                <Country>IRL</Country>
                <PerformanceName>Music and Arts</PerformanceName>
                <VenueID ID="197"/>
            </Event>
    </EventsPoint>
    

    in the file 197.xml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using PHP cURL to fetch XML output from a URL. Here is
I am trying to download a CSV file from a URL using PHP Curl,
Scenario: Sending XML, generated using php, via cURL to an external server for parsing.
I'm using PHP cURL module to extract timestamp of a remote file via HTTP
I am using PHP CURL to fetch data from a server. The response can
I am getting some info from a https web server using PHP plus cURL.
I wish to issue the following curl request using php-curl: curl http://www.example.com/ -F file=@foo.ext
I have a need to rename a file after download using php cURL. Here's
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
I'm trying to extract the price from the bellow html page/link using php cURL

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.