Search code examples
phpstringif-statementforeachdomdocument

Manipulating extracted XML string data in PHP


I am extracting and manipulating data from a Vehicle VIN Decoder. How it works is you enter a vehicle's VIN and it returns data regarding that vehicle's specification.

I am running into some trouble with the engine data. Each data point is tied to an ID, and I extract it using a foreach loop. Engine displacement is tied to $techData[42] where for VIN: WAUUL78E38A092113 $techData[42]= "4.2L/254". I am only concerned with the "4.2L" value. How can I extract just the 4.2L and create a variable with it to then echo? I need this additional code to be flexible and work for any VIN I enter. techData[42] can equal 4.2L, 5L, 5.5L, and on and on. Any ideas regarding how I achieve this would be greatly appreciated. Thanks!

Here is my PHP which returns all the $techData[]:

<?php
 $xml = file_get_contents('note.xml');
 $dom = new DOMDocument();
 $dom->loadXML($xml);

 foreach ( $dom->getElementsByTagName('technicalSpecification') as $techSpecElement )   {
   foreach($techSpecElement->getElementsByTagName('value') as $valueElement) {
       foreach($valueElement->getElementsByTagName('styleId') as $styleIdElement) {
           // check the value of the styleId here
           if (in_array($styleIdElement->nodeValue, [$variable2])) {
               // if it matches, get the parent value element's value
               $id = $techSpecElement->getElementsByTagName('titleId')->item(0)->nodeValue;
               $techData[$id] = $valueElement->getAttribute("value");
           }
       }
   }
}

echo "<b>Displacement:</b> ".$techData[42]."<br>";

?>

Here is the XML I am pulling this displacement data from:

<technicalSpecification>
 <titleId>42</titleId>
  <value value="4.2L/254" condition="">
   <styleId>292015</styleId>
   <styleId>292016</styleId>
  </value>
</technicalSpecification>

Solution

  • As your not sure if it has the /, I've just split it according to the L instead. So this then just takes the first part (plus an L) as the result.

    I've changed it to use XPath, although it may look complicated, it's just a case of breaking it down to individual steps...

    $dom = new DOMDocument();
    $dom->load("note.xml");
    $xp = new DOMXPath($dom);
    
    $id = "42";
    $styleID = "292015";
    
    $nodes = $xp->evaluate("//technicalSpecification[titleId='$id']/value[styleId='$styleID']/@value");
    $displacement = explode("L", $nodes->item(0)->nodeValue);
    echo $displacement[0]."L";
    

    The expression //technicalSpecification[titleId='$id']/value[styleId='$styleID']/@value basically uses the various criteria (ie $id) to narrow down the elements your interested in, same with the $styleID (may be $variable2). The end result is a list of the value attributes from the matching elements. This is why it then uses $nodes->item(0)->nodeValue to get the first item.

    Update:

    For your current logic you would need something like...

    echo "<b>Displacement:</b> ".explode("L", $techData[42])."L<br>";