我的XML
<properties>
<property>
<refno>001</refno>
<status>active</status>
<type>SP</type>
<description>Text Text Text</description>
<images>
<image>Path_here</image>
<image>Path_here</image>
<image>Path_here</image>
</images>
</property>
<property>
<refno>002</refno>
<status>active</status>
<type>SP</type>
<description>Text Text Text</description>
<images>
<image>Path_here</image>
<image>Path_here</image>
<image>Path_here</image>
</images>
</property>
</properties>
我的PHP代码
if ($xml = simplexml_load_file($xml_file_here)) {
foreach($xml->property as $i) {
if ($i->refno == "1") {
echo $i->description."<br />".
echo $i->type;
}
}
}
我通常循环遍历XML以找到[refno]值。有没有办法在不需要循环的情况下搜索值?
答案 0 :(得分:1)
您可以使用XPath表达式:
// find property element that contains a refno element with "001"
foreach ($xml->xpath('//property[contains(refno, "001")]') as $node) {
echo (string)$node->description, "\n";
}