使用定制的Php从文件解析Xml文件

使用定制的Php从文件解析Xml文件,php,xml,Php,Xml,我有空闲的代码来解析整个xml。在某个地方我犯了错误,无法从xml文件中获得正确的列表。 共有40多个部分,每个部分都有不同的内容。 Xml-4k+行示例 XML <ItemList> <Section Index="0" Name="Swords"> <Item Index="0" Slot="0" SkillIndex="0" Name="Kris" /> <Item Index="1" Slot="4" SkillIndex="0" Nam

我有空闲的代码来解析整个xml。在某个地方我犯了错误,无法从xml文件中获得正确的列表。 共有40多个部分,每个部分都有不同的内容。 Xml-4k+行示例

XML

<ItemList>
    <Section Index="0" Name="Swords">
<Item Index="0" Slot="0" SkillIndex="0" Name="Kris" />
<Item Index="1" Slot="4" SkillIndex="0" Name="Blade" />
++++
++++
+++
++
+

    </Section>
    <Section Index="1" Name="Axes">
    <Item Index="0" Slot="0" SkillIndex="0" Name="Small Axe" />
    <Item Index="1" Slot="1" SkillIndex="0" Name="Hand Axe" />
    ++++
    ++++
    ++
    ++
    +
    </Section>
    sections goes more...
    </ItemList>
我的问题是如何获得如下输出。提前谢谢你的帮助

Section Index : 0 Name: Swords
Item Index:0 Name :Kris
Item Index:1 Name :Short Sword
end
Section Index : 1 Name: Axes
Item Index:0 Name :Small Axe
Item Index:1 Name :Hand Axe
++
end

go on...
编辑

Index: Name :
Index: Name :
Index: Name :
Index:0 Name :Swords
Index: Name :
Index:0 Name :Kris
Index: Name :
Index:1 Name :Short Sword
Index: Name :

代码更正,现在我可以得到我想要的。问题是我无法将其解析为txt文件。有没有将xml结果解析为txt的建议?

问题是当前代码在每次循环迭代中都执行
echo
构造。如果操作员条件如下所示,则应在
内执行输出:

 ...
    $xml->open($XmlFile);
    while ($xml->read()) {
        if ($xml->nodeType === XMLReader::ELEMENT && $xml->name == 'Item'){
            $node = $xml->expand();
            echo 'Index:'.$xml->getAttribute('Index').'  Name :'.$xml->getAttribute('Name').'<br>';
        }

    }
    ....
。。。
$xml->open($XmlFile);
而($xml->read()){
如果($xml->nodeType===XMLReader::ELEMENT&&$xml->name=='Item'){
$node=$xml->expand();
回显'Index:'.$xml->getAttribute('Index')。'Name:'.$xml->getAttribute('Name')。
'; } } ....
 ...
    $xml->open($XmlFile);
    while ($xml->read()) {
        if ($xml->nodeType === XMLReader::ELEMENT && $xml->name == 'Item'){
            $node = $xml->expand();
            echo 'Index:'.$xml->getAttribute('Index').'  Name :'.$xml->getAttribute('Name').'<br>';
        }

    }
    ....