处理 PHP 中提取的 XML 字符串数据
Manipulating extracted XML string data in PHP
我正在从车辆 VIN 解码器中提取和处理数据。它的工作原理是您输入车辆的 VIN 及其 returns 有关该车辆规格的数据。
我 运行 遇到引擎数据方面的问题。每个数据点都绑定到一个 ID,我使用 foreach
循环提取它。发动机排量与 $techData[42]
相关,其中 VIN: WAUUL78E38A092113
$techData[42]= "4.2L/254"
。我只关心 "4.2L"
值。我怎样才能只提取 4.2L
并用它创建一个变量然后 echo
?我需要这个额外的代码来灵活处理我输入的任何 VIN。 techData[42]
可以等于 4.2L、5L、5.5L 等等。关于我如何实现这一目标的任何想法将不胜感激。谢谢!
这是我的 PHP 其中 returns 所有 $techData[]
:
<?php
$xml = file_get_contents('note.xml');
$dom = new DOMDocument();
$dom->loadXML($xml);
foreach ( $dom->getElementsByTagName('technicalSpecification') as $techSpecElement ) {
foreach($techSpecElement->getElementsByTagName('value') as $valueElement) {
foreach($valueElement->getElementsByTagName('styleId') as $styleIdElement) {
// check the value of the styleId here
if (in_array($styleIdElement->nodeValue, [$variable2])) {
// if it matches, get the parent value element's value
$id = $techSpecElement->getElementsByTagName('titleId')->item(0)->nodeValue;
$techData[$id] = $valueElement->getAttribute("value");
}
}
}
}
echo "<b>Displacement:</b> ".$techData[42]."<br>";
?>
这是 XML 我从以下位置提取的位移数据:
<technicalSpecification>
<titleId>42</titleId>
<value value="4.2L/254" condition="">
<styleId>292015</styleId>
<styleId>292016</styleId>
</value>
</technicalSpecification>
因为你不确定它是否有/,我只是按照L来分割它。所以这只是将第一部分(加上一个 L)作为结果。
我把它改为使用 XPath,虽然它可能看起来很复杂,但它只是将它分解为单个步骤的情况...
$dom = new DOMDocument();
$dom->load("note.xml");
$xp = new DOMXPath($dom);
$id = "42";
$styleID = "292015";
$nodes = $xp->evaluate("//technicalSpecification[titleId='$id']/value[styleId='$styleID']/@value");
$displacement = explode("L", $nodes->item(0)->nodeValue);
echo $displacement[0]."L";
表达式 //technicalSpecification[titleId='$id']/value[styleId='$styleID']/@value
基本上使用各种条件(即 $id
)来缩小您感兴趣的元素范围,与 $styleID
相同(可能是 $variable2
).最终结果是来自匹配元素的 value
属性的列表。这就是为什么它然后使用 $nodes->item(0)->nodeValue
来获取第一项的原因。
更新:
对于您当前的逻辑,您需要类似...
echo "<b>Displacement:</b> ".explode("L", $techData[42])."L<br>";
我正在从车辆 VIN 解码器中提取和处理数据。它的工作原理是您输入车辆的 VIN 及其 returns 有关该车辆规格的数据。
我 运行 遇到引擎数据方面的问题。每个数据点都绑定到一个 ID,我使用 foreach
循环提取它。发动机排量与 $techData[42]
相关,其中 VIN: WAUUL78E38A092113
$techData[42]= "4.2L/254"
。我只关心 "4.2L"
值。我怎样才能只提取 4.2L
并用它创建一个变量然后 echo
?我需要这个额外的代码来灵活处理我输入的任何 VIN。 techData[42]
可以等于 4.2L、5L、5.5L 等等。关于我如何实现这一目标的任何想法将不胜感激。谢谢!
这是我的 PHP 其中 returns 所有 $techData[]
:
<?php
$xml = file_get_contents('note.xml');
$dom = new DOMDocument();
$dom->loadXML($xml);
foreach ( $dom->getElementsByTagName('technicalSpecification') as $techSpecElement ) {
foreach($techSpecElement->getElementsByTagName('value') as $valueElement) {
foreach($valueElement->getElementsByTagName('styleId') as $styleIdElement) {
// check the value of the styleId here
if (in_array($styleIdElement->nodeValue, [$variable2])) {
// if it matches, get the parent value element's value
$id = $techSpecElement->getElementsByTagName('titleId')->item(0)->nodeValue;
$techData[$id] = $valueElement->getAttribute("value");
}
}
}
}
echo "<b>Displacement:</b> ".$techData[42]."<br>";
?>
这是 XML 我从以下位置提取的位移数据:
<technicalSpecification>
<titleId>42</titleId>
<value value="4.2L/254" condition="">
<styleId>292015</styleId>
<styleId>292016</styleId>
</value>
</technicalSpecification>
因为你不确定它是否有/,我只是按照L来分割它。所以这只是将第一部分(加上一个 L)作为结果。
我把它改为使用 XPath,虽然它可能看起来很复杂,但它只是将它分解为单个步骤的情况...
$dom = new DOMDocument();
$dom->load("note.xml");
$xp = new DOMXPath($dom);
$id = "42";
$styleID = "292015";
$nodes = $xp->evaluate("//technicalSpecification[titleId='$id']/value[styleId='$styleID']/@value");
$displacement = explode("L", $nodes->item(0)->nodeValue);
echo $displacement[0]."L";
表达式 //technicalSpecification[titleId='$id']/value[styleId='$styleID']/@value
基本上使用各种条件(即 $id
)来缩小您感兴趣的元素范围,与 $styleID
相同(可能是 $variable2
).最终结果是来自匹配元素的 value
属性的列表。这就是为什么它然后使用 $nodes->item(0)->nodeValue
来获取第一项的原因。
更新:
对于您当前的逻辑,您需要类似...
echo "<b>Displacement:</b> ".explode("L", $techData[42])."L<br>";