从复杂的 XML 输出中创建并回显 PHP 中的数组

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

我正在尝试使用此 XML 输出中的一些数据创建一个数组。鉴于数据的结构方式,我发现它非常棘手。我试图用 MECHANICAL description 数据形成一个数组。我希望我的输出看起来像:

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

到目前为止,我一直在尝试这样调用机械描述:

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

?>   

这是 XML 我试图从中提取的一部分:

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

如有任何帮助,我们将不胜感激。

更新 1
我已经确定如何接收 standard 标签的所有 description。但是,我只想要 descriptionMECHANICAL 数据。这是我当前的代码,它遵循我过去的逻辑。我可以围绕这个写一个 IF 声明吗?

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

更新 2
@splash58 建议使用 xPath 是解决此问题的最佳方法。在他的指导下,这是我目前的代码。我仍然没有收到任何回复。

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

?>

你应该尝试做的是首先将给定的 XML String 转换为 XMLJSON 然后将其转换为 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'];
   }
}

我发现下面列出了标准标签中的所有描述。仍在尝试弄清楚如何只用 MECHANICAL header 调用那些。

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

}

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

您可以使用 xpath select 符合您条件的节点

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

更新

如果你想使用domDocument

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

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