Search code examples
phpxmlforeachdomdocument

Extracting XML Data with Foreach Loops, Results Inconsistent


I am extracting XML data using DOMDocument and foreach loops. I am pulling certain attributes and node values from the XML document and creating variables with that data. I am then echoing the variables.

I have successfully completed this for the first portion of the XML data that lives between the <VehicleDescription tags. However, using the same logic with data within the <style> tags, I have been having issues. Specially, the created variables won't echo unless they are in the foreach loop. See the code below for clarification.

My php:

<?php

  $vehiclexml = $_POST['vehiclexml'];

  $xml = file_get_contents($vehiclexml);
  $dom = new DOMDocument();
  $dom->loadXML($xml);

   //This foreach loop works perfectly, the variables echo below:

  foreach ($dom->getElementsByTagName('VehicleDescription') as $vehicleDescription){
    $year = $vehicleDescription->getAttribute('modelYear');
    $make = $vehicleDescription->getAttribute('MakeName');
    $model = $vehicleDescription->getAttribute('ModelName');
    $trim = $vehicleDescription->getAttribute('StyleName');
    $id = $vehicleDescription->getAttribute('id');
    $BodyType = $vehicleDescription->getAttribute('altBodyType');
    $drivetrain = $vehicleDescription->getAttribute('drivetrain');
    }

   //This foreach loop works; however, the variables don't echo below, the will only echo within the loop tags. How can I resolve this?

  foreach ($dom->getElementsByTagName('style') as $style){
    $displacement = $style->getElementsByTagName('displacement')->item(0)->nodeValue;
    }


  echo "<b>Year:</b> ".$year;
  echo "<br>";
  echo "<b>Make:</b> ".$make;
  echo "<br>";
  echo "<b>Model:</b> ".$model;
  echo "<br>";
  echo "<b>Trim:</b> ".$trim;
  echo "<br>";
  echo "<b>Drivetrain:</b> ".$drivetrain;
  echo "<br>";

  //Displacement will not echo
  echo "<b>Displacement:</b> ".$displacement;

?>

Here is the XML file it is pulling from:

<VehicleDescription country="US" language="en" modelYear="2019" MakeName="Toyota" ModelName="RAV4" StyleName="LE" id="1111"  altBodyType="SUV" drivetrain="AWD">
  <style modelYear="2019" name="Toyota RAV4 LE" passDoors="4">
        <make>Toyota</make>
        <model>RAV4</model>
        <style>LE</style>
        <drivetrain>AWD</drivetrain>
        <displacement>2.5 liter</displacement>
        <cylinders>4-cylinder</cylinders>
        <gears>8-speed</gears>
        <transtype>automatic</transtype>
        <horsepower>203</horsepower>
        <torque>184</torque>
     </style>
</VehicleDescription>

Any help or insight as to why variables from the first foreach loop echo but variables from the second don't would be greatly appreciated.

Thanks!


Solution

  • Just to post an alternative solution to the way you've fixed this.

    As you've, there are a couple of <stlye> tags, this means that the foreach will attempt to use all style tags. But as you know that you are after the contents of the first tag only, you can drop the foreach loop and use the item() method...

    $displacement = $dom->getElementsByTagName('style')->item(0)
            ->getElementsByTagName('displacement')->item(0)->nodeValue;
    

    This also applies to how you fetch the data from the <VehicleDescription> tag. Drop the foreach and use

    $vehicleDescription = $dom->getElementsByTagName('VehicleDescription')->item(0);