
[英]Dynamically coded if statement executed inside an XMLReader while loop (using PHP)
[英]PHP - XMLReader issue with an if statement
下面的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');
}}
这是一个名为products.xml的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>
$ reader-> next('product')的位置; 是错的。 它应该不在第一个括号内。
即
$reader->next('product');
}}
至
}
$reader->next('product');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.