PHP - Retrieving Only Certain XML Nodes and Saving to a file

7331

New Member
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:\[code\]<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>\[/code\]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 \[code\] $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 documentecho $docDest->saveXML();\[/code\]
 
Back
Top