PHP XML阅读器未找到带连字符的节点

时间:2014-12-31 21:30:27

标签: php xml xmlreader

我有以下PHP,它可以正常工作,并找到所有' Widgets'在我的xml文档中:

  if($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'manufacturer'){
     $reader->read();
       if($reader->value == 'Widget'){
          echo 'congrats you have found the widget';
       }

但是当我尝试以下操作时,我没有得到任何结果,而且我知道" blue-widget"在制造商节点的xml文档中存在多次:

  if($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'manufacturer'){
     $reader->read();
       if($reader->value == 'blue-widget'){
          echo 'congrats you have found the widget';
       }

这是否与制造商名称中的连字符有关?我似乎也无法找到"蓝色小部件"通过任何其他属性,我可以找到我尝试提取的任何其他命名产品。就好像XML Reader没有看到包含" blue-widget"。

的产品节点

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<catalog>
//I can find this one
<product>
<programname>Example</programname>
<programurl>http://www.example.com</programurl>
<catalogname>Product Catalog</catalogname>
<lastupdated>date</lastupdated>
<name>first widget</name>
<keywords>some stuff</keywords>
<description>Typical description</description>
<sku>20</sku>
<manufacturer>Widget</manufacturer>
<manufacturerid>63</manufacturerid>
<upc>2250</upc>
<currency>USD</currency>
<price>6.79</price>
<buyurl>a url</buyurl>
<impressionurl>another url</impressionurl>
<imageurl>yet another url</imageurl>
<advertisercategory>widgets</advertisercategory>
<format>clean</format>
<instock>yes</instock>
<condition>new</condition>
<standardshippingcost>free</standardshippingcost>
</product>

<product>
//I can't find this one
<programname>Example</programname>
<programurl>http://www.example.com</programurl>
<catalogname>Product Catalog</catalogname>
<lastupdated>date</lastupdated>
<name>first widget</name>
<keywords>some stuff</keywords>
<description>Typical description</description>
<sku>20</sku>
<manufacturer>blue-widget</manufacturer>
<manufacturerid>63</manufacturerid>
<upc>2199</upc>
<currency>USD</currency>
<price>6.79</price>
<buyurl>a url</buyurl>
<impressionurl>another url</impressionurl>
<imageurl>yet another url</imageurl>
<advertisercategory>widgets</advertisercategory>
<format>clean</format>
<instock>yes</instock>
<condition>new</condition>
<standardshippingcost>free</standardshippingcost>
</product>



</catalog>

1 个答案:

答案 0 :(得分:0)

我的xml脚本中存在解析错误。当我消除错误时,一切正常。

我无法找到&#34; blue-widget&#34;是因为它在错误发生后出现在XML文档中。

所以最终我的帖子的答案是:

首先彻底检查错误并隔离它们!您可能遇到编码问题并引用以下线程以获得可能的解决方案:

PHP generated XML shows invalid Char value 27 message