0

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>
5
  • 1
    Will it always have that forward slash? If so, you can always explode() that variable and use the first value, eg: explode("/",$techData[42])[0]); Commented Oct 17, 2018 at 15:28
  • Can you give a sample of the XML as relying on things like position 42 for a particular VIN isn't going to give you what your after (I think). Commented Oct 17, 2018 at 15:32
  • @zack6849 I need to check with my data provider if it will always have the forward slash. I would like to assume no just to be safe but I could be wrong. Commented Oct 17, 2018 at 15:37
  • @nigelren I edited the question to contain the excerpt of XML I am pulling from. All displacement data will always have the <titleId>42</titleId> Commented Oct 17, 2018 at 15:37
  • What's $variable2 ? Commented Oct 17, 2018 at 15:49

1 Answer 1

1

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>";
Sign up to request clarification or add additional context in comments.

3 Comments

If I wanted to keep my current logic rather than switching over to DOMXPath would it look something like: $displacement = explode("L", $techData[42]); followed by echo $displacement[0]; ?
Would you say switching over to DOMXPath would be more advantageous than my current app logic of extracting data with foreach loops?
XPath isn't as easy sometimes, but if you are going to work with XML it would be good to start to learn it. As you can see from the code it is much shorter, and you can do some quick searches with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.