在PHP中按顺序解析XML

在PHP中按顺序解析XML,php,xml,xml-parsing,simplexml,Php,Xml,Xml Parsing,Simplexml,我有一个XML字符串,如下所示: <?xml version="1.0" encoding="utf-8"?> <document id="0" name="RegSimple" Version="0.1"> <Description> Configuration document for simple registration of incidents. This document will contain both the fixed an

我有一个XML字符串,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<document id="0" name="RegSimple" Version="0.1">
<Description>
    Configuration document for simple registration of incidents.
    This document will contain both the fixed and the user-defined fields to
    show on the registration screen.
</Description>
<Fields>
    <Textbox id="1" name="IncidentID" visibility="Hidden" width="500"
        displayreadonly="False" />
    <Comment id="200" height="10" name="c1" caption=" " />
    <Header id="100" name="Header" caption="Description" />
    <Radiobox id="2" name="Type" caption="Feedback type" width="500"
        type="19" displayreadonly="true"></Radiobox>
    <Textbox id="3" name="Description" caption="My feedback"
        width="500" height="75" multiline="True" required="True"
        displayreadonly="True" />
    <Textbox id="4" name="Incident" caption="Title *" width="500"
        height="25" multiline="False" required="True" displayreadonly="False" />
    <Comment id="201" width="500" name="Comment2"
        caption="* All messages are published with a short title that describes the feedback"
        multiline="True" height="40" />
    <Comment id="202" height="20" name="c1" caption=" " />
    <Combobox id="5" name="Unit" caption="Unit" width="500" type="12"
        required="True" displayreadonly="True"></Combobox>
    <Combobox id="6" name="Country" caption="Country" width="500"
        type="11" required="True" displayreadonly="True"></Combobox>
    <Textbox id="7" name="Office" caption="Office" height="50"
        width="500" multiline="True" displayreadonly="True" />
    <Textbox id="8" name="Cause" caption="Cause" height="50"
        width="500" multiline="True" displayreadonly="True" />
    <Textbox id="9" name="Action" caption="Recommended actions"
        height="50" width="500" multiline="True" displayreadonly="True" />
    <Combobox id="10" name="Theme" caption="Theme" width="500"
        type="3" required="True" displayreadonly="True"></Combobox>
    <Header id="102" name="Time" caption="Date, Time and location" />
    <Datetime id="11" name="Date" caption="Date" width="120"
        displayreadonly="True" />
    <Textbox id="12" name="Time" caption="Time" width="120"
        displayreadonly="True" property="True" />
    <Textbox id="13" name="Location" caption="Location" width="500"
        displayreadonly="True" />
    <Header id="103" name="info" caption="Contact information" />
    <Textbox id="14" name="Name" caption="Name" width="500" tag="user.name"
        displayreadonly="False" />
    <Textbox id="15" name="E-Mail" caption="E-Mail" width="500"
        tag="user.e-mail" displayreadonly="False" />
    <Textbox id="16" name="Registered date" visibility="Hidden"
        width="500" tag="system.date" displayreadonly="False" />
</Fields>
</document>
在这个结果中,所有文本框元素都在一个数组中返回。类似地,所有注释组合框都以单独的数组返回,而与原始XML中的顺序无关

有人能告诉我如何按元素的实际顺序解析元素吗? 谢谢。

您可以使用和遍历树:

$tree = new RecursiveIteratorIterator(
    new SimpleXmlIterator($xml),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($tree as $node) {
    echo $node->getName(), PHP_EOL;
}
将输出()


您需要不断遍历XML元素树。每个分支都是另一个简单的xml元素,您可以在foreach LLOP中对它们进行详细说明

<?php

/* For each <character> node, we echo a separate <name>. */
foreach ($xml as $element) {
    $elementName = $element->getName();
    $elementAttributes = $element->attributes();
    if ($element->children()) {
        foreach ($element->children() as $subElement) {
          // etc
        }
    }
}

?> 
getName();
$elementAttributes=$element->attributes();
如果($element->children()){
foreach($element->children()作为$subElement){
//等
}
}
}
?> 

这很有效。您能告诉我如何获取叶节点的属性吗?@Arvind07请看,您能告诉我如何循环所有属性吗?@Arvind07您应该能够从我已经给您的链接中了解到这一点。
Description
Fields
Textbox
Comment
Header
Radiobox
Textbox
Textbox
Comment
Comment
Combobox
Combobox
Textbox
Textbox
Textbox
Combobox
Header
Datetime
Textbox
Textbox
Header
Textbox
Textbox
Textbox
<?php

/* For each <character> node, we echo a separate <name>. */
foreach ($xml as $element) {
    $elementName = $element->getName();
    $elementAttributes = $element->attributes();
    if ($element->children()) {
        foreach ($element->children() as $subElement) {
          // etc
        }
    }
}

?>