编程语言
首页 > 编程语言> > PHP XMLReader解析行两次

PHP XMLReader解析行两次

作者:互联网

我正在解析一个xml文件.例如,当我输出一个属性时,我总是得到2次结果.

这是我所做的一些简化代码:

    $xml = new XMLReader();

    $xml->open($file);

    while ($xml->read()) {
        if ($xml->name == 'file')
            echo $xml->getAttribute ('Product_ID') . '<br />';
    }

    // close stream
    $xml->close();

这就是我得到的:

1980
1980
37444
37444
45287
45287
65438
65438
76916
76916
101158
101158
271287
271287

XML结构如下:

<file path="export/freexml.int/DE/15986140.xml" Product_ID="15986140" Updated="20121114141132" Quality="ICECAT" Supplier_id="728" Prod_ID="RBBD2MZ" Catid="2282" On_Market="0" Model_Name="ThinkCentre Edge 92z" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15916192-2729.jpg" HighPicSize="12635" HighPicWidth="337" HighPicHeight="294" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986142.xml" Product_ID="15986142" Updated="20121114143018" Quality="ICECAT" Supplier_id="24" Prod_ID="NX.C0ZEB.002" Catid="151" On_Market="0" Model_Name="TE11HC-32376G50Mnks" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986142-574.jpg" HighPicSize="179174" HighPicWidth="786" HighPicHeight="621" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986149.xml" Product_ID="15986149" Updated="20121114144736" Quality="ICECAT" Supplier_id="24" Prod_ID="NX.C1UEB.001" Catid="151" On_Market="0" Model_Name="LE11-BZ-E1124G50Mn" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986149-2702.jpg" HighPicSize="205805" HighPicWidth="786" HighPicHeight="621" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986153.xml" Product_ID="15986153" Updated="20121114200420" Quality="ICECAT" Supplier_id="1935" Prod_ID="50203" Catid="194" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986153-3865.jpg" HighPicSize="1928713" HighPicWidth="2751" HighPicHeight="1897" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986154.xml" Product_ID="15986154" Updated="20121114200048" Quality="ICECAT" Supplier_id="1935" Prod_ID="ARMAKB" Catid="194" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986154-7619.jpg" HighPicSize="1928713" HighPicWidth="2751" HighPicHeight="1897" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986155.xml" Product_ID="15986155" Updated="20121114194744" Quality="ICECAT" Supplier_id="1935" Prod_ID="ARMAM" Catid="195" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986155-4238.jpg" HighPicSize="639005" HighPicWidth="2201" HighPicHeight="3265" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986156.xml" Product_ID="15986156" Updated="20121114194735" Quality="ICECAT" Supplier_id="1935" Prod_ID="54577" Catid="195" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986156-7292.jpg" HighPicSize="639005" HighPicWidth="2201" HighPicHeight="3265" Date_Added="20121114000000"></file>

如您所见,每个数字都会显示两次.我没有问题-.-我究竟做错了什么?

非常感谢您的帮助!

//编辑

好的,我这样修复:

if ($xml->name == 'file' && $xml->nodeType == XMLReader::ELEMENT)

谢谢您的帮助 !

解决方法:

在文档的注释中找到了这一点:

might be obvious, but not to everyone

标签:xmlreader,output,xml,php
来源: https://codeday.me/bug/20191031/1977265.html