Search code examples
phparraysxmldomdocument

Creating and Echoing an Array in PHP from a Complex XML Output


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

?>

Solution

  • 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 ;
    }