Jump to content

Parsing XML


terrid25

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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
                )

        )

)

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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
                )

        )

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.