使用 DOMDocument 提取 XML 个值

Using DOMDocument to Pull XML Values

我有一个 XML 格式的 Soap Envelope 响应,我正试图从中提取数据。我已经成功地提取了一些数据点;但是,我正在与一对夫妇作斗争。到目前为止,除了 getElementsbyTagName() 函数外,我还使用了 DOMDocument

我正在尝试提取 <technicalSpecification> 数据。主要是 "Audi S4"、“5 Door Wagon”、"All-Wheel Drive"、"Small Station Wagon" 和“5.0”。

这里是 XML (note.xml):

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
  <VehicleDescription country="US" language="en" modelYear="2008" bestMakeName="Audi" bestModelName="S4" bestStyleName="5dr Avant Wgn" xmlns="urn:description7b.services.chrome.com">
     <responseStatus responseCode="Successful" description="Successful"/>
     <vinDescription vin="WAUUL78E38A092113" modelYear="2008" division="Audi" modelName="S4" styleName="5dr Avant Wgn" bodyType="Wagon 4 Dr." drivingWheels="AWD" builddata="no">
        <WorldManufacturerIdentifier>Germany Audi Nsu</WorldManufacturerIdentifier>
        <restraintTypes>
           <group id="9">Safety</group>
           <header id="38">Air Bag - Frontal</header>
           <category id="1001">Driver Air Bag</category>
        </restraintTypes>
        <restraintTypes>
           <group id="9">Safety</group>
           <header id="38">Air Bag - Frontal</header>
           <category id="1002">Passenger Air Bag</category>
        </restraintTypes>
        <restraintTypes>
           <group id="9">Safety</group>
           <header id="39">Air Bag - Side</header>
           <category id="1005">Front Side Air Bag</category>
        </restraintTypes>
        <restraintTypes>
           <group id="9">Safety</group>
           <header id="39">Air Bag - Side</header>
           <category id="1007">Front Head Air Bag</category>
        </restraintTypes>
        <restraintTypes>
           <group id="9">Safety</group>
           <header id="39">Air Bag - Side</header>
           <category id="1008">Rear Head Air Bag</category>
        </restraintTypes>
        <marketClass id="53">Small Wagon</marketClass>
     </vinDescription>
     <technicalSpecification>
        <titleId>1</titleId>
        <value value="Audi S4" condition="-PT">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
        <value value="Audi S4" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>2</titleId>
        <value value="5 Door Wagon" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>6</titleId>
        <value value="All-Wheel Drive" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>7</titleId>
        <value value="Small Station Wagon" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>8</titleId>
        <range min="5.0" max="5.0"/>
        <value value="5" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
  </VehicleDescription>

这是我的 PHP:

<html>
 <body>

  <?php

  $xml = file_get_contents('note.xml');

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


  $id = $dom->getElementsByTagName('WorldManufacturerIdentifier')->item(0);
  $vin = $dom->getElementsByTagName('titleId')->item(0);

  echo $id->textContent;
  echo "<br>";
  echo $vin->textContent;


  ?>

 </body>
</html>

这个returns:

Germany Audi Nsu
1

我想要的 return 是:

Germany Audi Nsu
Audi S4
5 Door Wagon
All-Wheel Drive
Small Station Wagon
5.0

任何关于如何调用这些 technicalSpecification 变量的想法都很棒。非常感谢!

___________________________________________________________________________

我现在正尝试从“外观颜色”部分提取数据。我很亲近。我希望输出如下所示:

常用颜色名称 - 技术颜色名称
所以像这样:

红色 - 艳丽的红色
黑色 - 亮黑色

这是外部颜色输出的 XML:

     <exteriorColor colorCode="A2A2" colorName="Brilliant Black" rgbValue="131313">
        <genericColor name="Black" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="C8C8" colorName="Brilliant Red" rgbValue="D23637">
        <genericColor name="Red" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="8A8A" colorName="Deep Sea Blue Pearl" rgbValue="0F172A">
        <genericColor name="Blue" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="F5F5" colorName="Dolphin Gray Metallic" rgbValue="53575F">
        <genericColor name="Gray" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="T9T9" colorName="Ibis White" rgbValue="F4F5EF">
        <genericColor name="White" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="1T1T" colorName="Imola Yellow" rgbValue="FFE242">
        <genericColor name="Yellow" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="5B5B" colorName="Light Silver Metallic" rgbValue="A4A5A7">
        <genericColor name="Gray" primary="false"/>
        <genericColor name="Silver" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="L8L8" colorName="Phantom Black Pearl" rgbValue="000000">
        <genericColor name="Black" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>
     <exteriorColor colorCode="5N5N" colorName="Sprint Blue Pearl" rgbValue="1F3986">
        <genericColor name="Blue" primary="true"/>
        <styleId>292015</styleId>
        <styleId>292016</styleId>
     </exteriorColor>

到目前为止,这是我的 PHP:

$finalColor = [];
foreach ($dom->getElementsByTagName('exteriorColor') as $exColor){
   $genColor = $exColor->getElementsByTagName('genericColor')->item(0)->getAttribute("name");
   $colorNames = $exColor->getAttribute("colorName");
   $finalColor = $genColor."- ".$colorNames;
 }
   print_r($finalColor);
   echo "<br>";

这只会检索最后一种颜色,而不是整个数组。调整了一段时间,没有成功。有什么想法吗?

您可以使用与现有相同的想法 - 使用 getElementsByTagName() 获取 <technicalSpecification> 元素的列表,然后在其中提取 <value> 元素。使用元素的 ->getAttribute("value") 提取您之后的值。

foreach ( $dom->getElementsByTagName('technicalSpecification') as $techSpec )   {
    echo $techSpec->getElementsByTagName('value')->item(0)->getAttribute("value");
    echo "<br>";
}

哪个输出...

Audi S4<br>5 Door Wagon<br>All-Wheel Drive<br>Small Station Wagon<br>5<br>

唯一的问题是最后一个值是 5 而不是 5.0,因为它与其他项目不在同一个字段中。

更新:

您可以使用 <titleId> 值作为索引并使用上面提取的值作为它的值来存储所有这些数据。因此,只需在遍历时建立一个数组...

$techData = [];
foreach ( $dom->getElementsByTagName('technicalSpecification') as $techSpec )   {
    $id = $techSpec->getElementsByTagName('titleId')->item(0)->nodeValue;
    $techData [$id]= $techSpec->getElementsByTagName('value')->item(0)->getAttribute("value");
}
print_r($techData);
echo $techData[7];

这给了你...

Array
(
    [1] => Audi S4
    [2] => 5 Door Wagon
    [6] => All-Wheel Drive
    [7] => Small Station Wagon
    [8] => 5
)
Small Station Wagon

您可以使用 XPath 来做同样的事情,但如果您使用超过 1 个字段,这样做可能更容易(恕我直言)。