terrid25 Posted March 28, 2011 Share Posted March 28, 2011 Hi I have an XML file that I've been trying to parse with some success. <?xml version='1.0' encoding='ISO-8859-1'?> <Notes> <Madrid> <trip> <ID>12518980</ID> <Duration>130</Duration> </trip> <trip> <ID>12518981</ID> <Duration>600</Duration> </trip> <trip> <ID>12518982</ID> <Duration>50</Duration> </trip> </Madrid> <London> <trip> <ID>12518983</ID> <Duration>Suzuki</Duration> </trip> </London> <Chicago> <trip> <ID>12518984</ID> <Duration>1600</Duration> </trip> <Chicago> </Notes> I am able to get all the nodes that I want, which is mainly ID and duration using: $objDOM = new DOMDocument(); $objDOM->load("data.xml"); $note = $objDOM->getElementsByTagName("trip"); foreach( $note as $value ) { $ids = $value->getElementsByTagName("ID"); $id = $ids->item(0)->nodeValue; $durations = $value->getElementsByTagName("Duration"); $duration = $duration->item(0)->nodeValue; echo "ID is:".$id."<br />"; echo "Duration is:".$duration."<br />"; } What I'd really like to do, is to the get duration and ID for each of the parent nodes, i.e. Mardid, London and Chicago, without having 3 separate foreach loops, if that is possible? Quote Link to comment Share on other sites More sharing options...
silkfire Posted March 28, 2011 Share Posted March 28, 2011 You only have one foreach loop, what's the problem? Quote Link to comment Share on other sites More sharing options...
silkfire Posted March 28, 2011 Share Posted March 28, 2011 Like this? Array ( [Madrid] => Array ( [0] => Array ( [iD] => 12518980 [Duration] => 130 ) [1] => Array ( [iD] => 12518981 [Duration] => 600 ) [2] => Array ( [iD] => 12518982 [Duration] => 50 ) ) [London] => Array ( [0] => Array ( [iD] => 12518983 [Duration] => Suzuki ) ) [Chicago] => Array ( [0] => Array ( [iD] => 12518984 [Duration] => 1600 ) ) ) Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 28, 2011 Share Posted March 28, 2011 $objDOM = new DOMDocument(); $objDOM->load("data.xml"); $cities = $objDOM->getElementsByTagName("Notes"); foreach($cities as $city) { $trip = $city->getElementsByTagName("trip"); foreach($trip as $value) { // Get each trip's details, $city having the parent node's name. } } It's just pseudo code, but it demonstrates the logic. Quote Link to comment Share on other sites More sharing options...
silkfire Posted March 28, 2011 Share Posted March 28, 2011 <pre> <? $xml = '<Notes> <Madrid> <trip> <ID>12518980</ID> <Duration>130</Duration> </trip> <trip> <ID>12518981</ID> <Duration>600</Duration> </trip> <trip> <ID>12518982</ID> <Duration>50</Duration> </trip> </Madrid> <London> <trip> <ID>12518983</ID> <Duration>Suzuki</Duration> </trip> </London> <Chicago> <trip> <ID>12518984</ID> <Duration>1600</Duration> </trip> </Chicago> </Notes>'; $xmlObject = simplexml_load_string($xml); $results = array(); foreach($xmlObject->children() as $city) { foreach($city->children() as $trip) $results[$city->getName()][] = array('ID' => (string)$trip->ID, 'Duration' => (string)$trip->Duration); } print_r($results); ?> </pre> Quote Link to comment Share on other sites More sharing options...
terrid25 Posted March 30, 2011 Author Share Posted March 30, 2011 Like this? Array ( [Madrid] => Array ( [0] => Array ( [iD] => 12518980 [Duration] => 130 ) [1] => Array ( [iD] => 12518981 [Duration] => 600 ) [2] => Array ( [iD] => 12518982 [Duration] => 50 ) ) [London] => Array ( [0] => Array ( [iD] => 12518983 [Duration] => Suzuki ) ) [Chicago] => Array ( [0] => Array ( [iD] => 12518984 [Duration] => 1600 ) ) ) Yeah something like that, but, if possible, in each of the array, could I also pull in the city? [Chicago] => Array ( [0] => Array ( [iD] => 12518984 [Duration] => 1600 [City] => Chicago ) ) Quote Link to comment Share on other sites More sharing options...
silkfire Posted March 30, 2011 Share Posted March 30, 2011 Easy: <pre> <? $xml = '<Notes> <Madrid> <trip> <ID>12518980</ID> <Duration>130</Duration> </trip> <trip> <ID>12518981</ID> <Duration>600</Duration> </trip> <trip> <ID>12518982</ID> <Duration>50</Duration> </trip> </Madrid> <London> <trip> <ID>12518983</ID> <Duration>Suzuki</Duration> </trip> </London> <Chicago> <trip> <ID>12518984</ID> <Duration>1600</Duration> </trip> </Chicago> </Notes>'; $xmlObject = simplexml_load_string($xml); $results = array(); foreach($xmlObject->children() as $city) { foreach($city->children() as $trip) $results[$city->getName()][] = array('ID' => (string)$trip->ID, 'Duration' => (string)$trip->Duration, 'City' => $city->getName()); } print_r($results); ?> </pre> Quote Link to comment Share on other sites More sharing options...
terrid25 Posted March 31, 2011 Author Share Posted March 31, 2011 Easy: <pre> <? $xml = '<Notes> <Madrid> <trip> <ID>12518980</ID> <Duration>130</Duration> </trip> <trip> <ID>12518981</ID> <Duration>600</Duration> </trip> <trip> <ID>12518982</ID> <Duration>50</Duration> </trip> </Madrid> <London> <trip> <ID>12518983</ID> <Duration>Suzuki</Duration> </trip> </London> <Chicago> <trip> <ID>12518984</ID> <Duration>1600</Duration> </trip> </Chicago> </Notes>'; $xmlObject = simplexml_load_string($xml); $results = array(); foreach($xmlObject->children() as $city) { foreach($city->children() as $trip) $results[$city->getName()][] = array('ID' => (string)$trip->ID, 'Duration' => (string)$trip->Duration, 'City' => $city->getName()); } print_r($results); ?> </pre> Excellent! Thanks! How do I access each element though, i.e. echo "ID is: .$variable"; echo "Duration is: .$variable"; echo "City is: .$variable"; for each of the array items? Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.