我有一个XML格式的Soap Envelope响应,我正在尝试从中提取数据。我已经成功提取了一些数据点;但是,我正在与一对夫妇奋斗。到目前为止,除了DOMDocument
函数之外,我还使用了getElementsbyTagName()
。
我正在尝试提取<technicalSpecification>
数据。主要是“奥迪S4”,“ 5门旅行车”,“全轮驱动”,“小型旅行车”和“ 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>
这将返回:
Germany Audi Nsu
1
我理想的回报是:
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>";
这仅检索最后一种颜色,而不检索整个数组。对其进行了一段时间的调整,但没有成功。有什么想法吗?
答案 0 :(得分:1)
您可以使用与已有的想法相同的方法-使用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进行相同的操作,但是如果您使用多个字段,则以这种方式(IMHO)做起来可能会更容易。