1

I am trying to create an array with some of the data from this XML output. Given the way the data is structured I have found it to be quite tricky. I am trying to form an array with just the MECHANICAL description data. I would want my output to look something like:

Mechanical Engine: 2.0L DOHC 4-Cylinder TFSI Transmission: 8-Speed Automatic w/Tirptronic Full-Time All-Wheel Drive and so on...

So far I have been trying to call the mechanical descriptions like this:

<?php

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

foreach ($dom->getElementsByTagName('standard') as $standard){
  $mechdescription = $standard->getElementsByTagName('description');
  $mecharray[] = $mechdescription;
}
foreach ($mecharray as $displaymech){
  echo $displaymech;
}

?>   

Here is a portion of the XML I am trying to pull from:

    <VehicleDescription xmlns="urn:description7b.services.chrome.com" country="US" language="en" modelYear="2015" bestMakeName="Audi" bestModelName="A4" bestStyleName="4dr Sdn Auto quattro 2.0T Premium" bestTrimName="Premium">
      <standard>
        <header id="1236">MECHANICAL</header>
        <description>Engine: 2.0L DOHC 4-Cylinder TFSI -inc: Audi valvelift system</description>
        <category id="1048"/>
        <category id="1054"/>
        <category id="1213"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Transmission: 8-Speed Automatic w/Tiptronic -inc: sport program and manual shift mode</description>
        <category id="1130"/>
        <category id="1195"/>
        <category id="1220"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Full-Time All-Wheel Drive</description>
        <category id="1041"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Engine Oil Cooler</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>80-Amp/Hr Maintenance-Free Battery w/Run Down Protection</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>150 Amp Alternator</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Gas-Pressurized Shock Absorbers</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Front And Rear Anti-Roll Bars</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Electric Power-Assist Speed-Sensing Steering</description>
        <category id="1084"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>16.1 Gal. Fuel Tank</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Single Stainless Steel Exhaust</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Multi-Link Front Suspension w/Coil Springs</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>Multi-Link Rear Suspension w/Coil Springs</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1236">MECHANICAL</header>
        <description>4-Wheel Disc Brakes w/4-Wheel ABS, Front Vented Discs, Brake Assist, Hill Hold Control and Electric Parking Brake</description>
        <category id="1018"/>
        <category id="1020"/>
        <category id="1228"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Wheels: 8.0J x 17" 10-Spoke-Star-Design</description>
        <category id="1123"/>
        <styleId>369101</styleId>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Tires: P245/45R17 AS</description>
        <category id="1092"/>
        <category id="1097"/>
        <styleId>369101</styleId>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Wheels w/Silver Accents</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Steel Spare Wheel</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Compact Spare Tire Mounted Inside Under Cargo</description>
        <category id="1098"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Clearcoat Paint</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Express Open/Close Sliding And Tilting Glass 1st Row Sunroof w/Sunshade</description>
        <category id="1069"/>
        <category id="1132"/>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Body-Colored Front Bumper</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Body-Colored Rear Bumper</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Rocker Panel Extensions</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
     <standard>
        <header id="1176">EXTERIOR</header>
        <description>Aluminum Side Windows Trim and Black Front Windshield Trim</description>
        <styleId>369101</styleId>
        <installed cause="BaseEquipment"/>
     </standard>
 </VehicleDescription>

Any help would be greatly appreciated.

UPDATE 1
I have determined how to receive all descriptions for standard tags. However, I only want description's for MECHANICAL data. Here is my current code which follows my past logic. Can I write an IF statement around this?

foreach ($dom->getElementsByTagName('standard') as $standard){
  $mechdescription = $standard->getElementsByTagName('description')->item(0)->nodeValue;
  $mecharray[] = $mechdescription;
}
foreach ($mecharray as $displaymech){
  echo $displaymech."<br>";
}

UPDATE 2
@splash58 has suggested using xPath is best way to tackle this. With his guidance this is my current code. I am still not receiving any response.

<?php

 $xml = file_get_contents('note.xml');
 $dom = new DOMDocument();
 $dom->loadXML($xml);
 $xpath = new domXpath($dom);

 foreach ($xpath->query('//standard[header="MECHANICAL"]/description') as $mechdescription){
 $mecharray[] = $mechdescription->nodeValue ;
}
 foreach ($mecharray as $mechanicaldescription){
 echo $mechanicaldescription;
}

?>

3 Answers 3

1

You can use xpath to select a node with your condition

$dom = simplexml_load_string($xml);
foreach ($dom->xpath('//standard[header="MECHANICAL"]/description') as $mechdescription){
   $mecharray[] = $mechdescription;
}

UPDATE

If you want to use domDocument

$dom = new DOMDocument(); 
$dom->loadXML($xml);
$xpath = new domXpath($dom);

foreach ($xpath->query('//standard[header="MECHANICAL"]/description') as $mechdescription){
  $mecharray[] = $mechdescription->nodeValue ;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am not getting a response when I add this to my app. Note how I currently define DOM is: $dom = new DOMDocument(); and $dom->loadXML($xml);
See Update 2 in my original question for further details. Still not receiving an output with this code.
I found why I am getting an error. The XML document is bounded by a <VehicleDescription> tag. I tried modifying the foreach ($xpath->query('//VehicleDescription/standard[header="MECHANICAL"]/description') with VehicleDescription with no success. Thanks again for all your help!
Any ideas as to why the code throws an error when I add VehicleDescription tags around the XML? sandbox.onlinephpfunctions.com/code/…
0

I have found that the following lists all the descriptions within the standard tags. Still trying to figure how to call those with just the MECHANICAL header.

foreach ($dom->getElementsByTagName('standard') as $standard){
  $mechdescription = $standard->getElementsByTagName('description')->item(0)->nodeValue;
  $mecharray[] = $mechdescription;

}

foreach ($mecharray as $displaymech){
  echo $displaymech."<br>";
}

Comments

0

What you should try to do is first convert given XML String to XML to JSON and then convert it to an Array.

$pureXml = simplexml_load_string($strXml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($pureXml);
$arr = json_decode($json,TRUE);
foreach($arr as $subArr){
   if($subArr['header'] == 'MECHANICAL'){
       print $subArr['header] . "<br>" . $subArr['description'];
   }
}

2 Comments

I have determined how to create an array out of all the data within the standard tags. (See most recent answer). I am struggling to call the standard descriptions that have the MECHANICAL header tag.
@mugelloblue I have updated my answer to reflect this.

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.