【问题标题】:PHP - XMLReader issue with an if statementPHP - 带有 if 语句的 XMLReader 问题
【发布时间】:2014-04-16 23:48:52
【问题描述】:

下面的 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>

【问题讨论】:

    标签: php xpath xmlreader


    【解决方案1】:

    $reader的位置->next('product');是错的。 它应该在第一个大括号之外。

    $reader->next('product');
    }}
    

    }
    $reader->next('product');
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多