【问题标题】:Creating and Echoing an Array in PHP from a Complex XML Output在 PHP 中从复杂的 XML 输出创建和回显数组
【发布时间】:2019-03-05 09:05:12
【问题描述】:

我正在尝试使用此 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
我已经确定如何接收所有descriptions 的standard 标签。但是,我只想要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;
}

?>

【问题讨论】:

    标签: php arrays xml domdocument


    【解决方案1】:

    你应该尝试做的是首先将给定的 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'];
       }
    }
    

    【讨论】:

    • 我已经确定如何使用standard 标签内的所有数据创建一个数组。 (请参阅最新答案)。我正在努力调用具有MECHANICAL 标头标签的standard 描述。
    • @mugelloblue 我已经更新了我的答案以反映这一点。
    【解决方案2】:

    我发现下面列出了标准标签中的所有描述。仍在试图弄清楚如何仅使用 MECHANICAL 标头来调用那些。

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 当我将它添加到我的应用程序时,我没有得到响应。请注意我目前如何定义 DOM 是:$dom = new DOMDocument();$dom-&gt;loadXML($xml);
      • 请参阅我原来的问题中的 Update 2 了解更多详情。仍然没有收到带有此代码的输出。
      • 我找到了错误的原因。 XML 文档以&lt;VehicleDescription&gt; 标记为界。我尝试用VehicleDescription 修改foreach ($xpath-&gt;query('//VehicleDescription/standard[header="MECHANICAL"]/description'),但没有成功。再次感谢您的帮助!
      • 当我在 XML 周围添加 VehicleDescription 标签时,为什么代码会抛出错误? sandbox.onlinephpfunctions.com/code/…
      猜你喜欢
      • 2021-01-11
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2016-08-05
      • 2013-04-20
      • 1970-01-01
      相关资源
      最近更新 更多