下面的XMLReader / Xpath代码可以正常工作而没有if语句(在第二个while循环中)。当放入if语句时,页面甚至不会加载。关于if语句有什么问题的任何建议?谢谢!
$reader = new XMLReader;
$reader->open('products.xml');
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
while ($reader->read() && $reader->name !== 'product') {
continue;
}
while ($reader->name === 'product') {
$node = $dom->importNode($reader->expand(), TRUE);
if ($xpath->evaluate('number(price)', $node) < 500) {
$category = $xpath->evaluate('string(@category)', $node);
$name = $xpath->evaluate('string(name)', $node);
$price = $xpath->evaluate('number(price)', $node);
echo "Category: " . $category . ". ";
echo "Name: " . $name . ". ";
echo "Price: " . $price . ". ";
echo "<br>";
$reader->next('product');
}}
以下是XML文件的位代码,名为products.xml:
<products>
<product category="Desktop">
<name> Desktop 1 (d)</name>
<price>499.99</price>
</product>
<product category="Tablet">
<name>Tablet 1 (t)</name>
<price>1099.99</price>
</product>
</products>
答案 0 :(得分:0)
$ reader-&gt; next('product')的位置;是错的。 它应该是第一个支撑。
即
$reader->next('product');
}}
到
}
$reader->next('product');
}