PHP SimpleXmlElement 分组空 XML 元素

PHP SimpleXmlElement Grouping Empty XML Elements

我正在从 Web 服务获取 XML,但很难维持显示的 XML 元素的顺序。

这是一个片段:

    <Blocks>
      <Block>
         <BlockElements>
            <Question QuestionID="QID59" />
            <PageBreak />
            <Question QuestionID="QID3" />
            <Question QuestionID="QID1" />
            <PageBreak />
            <Question QuestionID="QID2" />
            <PageBreak />
            <Question QuestionID="QID47" />
         </BlockElements>
      </Block>
      <Block>
         <BlockElements>
            <Question QuestionID="QID6" />
            <PageBreak />
            <Question QuestionID="QID8" />
            <Question QuestionID="QID49" />
            <Question QuestionID="QID45" />
            <Question QuestionID="QID50" />
            <PageBreak />
            <Question QuestionID="QID9" />
            <Question QuestionID="QID10" />
            <PageBreak />
            <Question QuestionID="QID11" />
            <Question QuestionID="QID12" />
            <PageBreak />
            <Question QuestionID="QID13" />
            <PageBreak />
            <Question QuestionID="QID14" />
            <PageBreak />
            <Question QuestionID="QID15" />
            <PageBreak />
            <Question QuestionID="QID16" />
            <PageBreak />
            <Question QuestionID="QID17" />
            <PageBreak />
            <Question QuestionID="QID18" />
            <Question QuestionID="QID19" />
            <PageBreak />
            <Question QuestionID="QID20" />
            <Question QuestionID="QID21" />
            <PageBreak />
            <Question QuestionID="QID22" />
            <Question QuestionID="QID23" />
         </BlockElements>
      </Block>
    </Blocks>

Web 服务使用空元素和(没有结束标记的元素)。这些元素在 BlockElements 标签中的顺序是至关重要的——它定义了表单的布局。当我使用 SimpleXmlElement 将此 XML 转换为 PHP 对象时,这就是我得到的:

SimpleXMLElement Object
(
    [Block] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [BlockElements] => SimpleXMLElement Object
                        (
                            [Question] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID59
                                                )
                                        )
                                    [1] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID3
                                                )
                                        )
                                    [2] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID1
                                                )
                                        )
                                    [3] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID2
                                                )
                                        )
                                    [4] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID47
                                                )
                                        )
                                )
                            [PageBreak] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                        )
                                    [1] => SimpleXMLElement Object
                                        (
                                        )
                                    [2] => SimpleXMLElement Object
                                        (
                                        )
                                )
                        )
                )
....

Question 元素和 PageBreak 元素被分隔到数组中,并且它们的顺序丢失了。关于维护这些元素的顺序有什么建议吗?我不打算使用 SimpleXmlElement - 我会使用任何有效的方法。谢谢!

您必须知道该对象的迭代结果与 'print_r' 或 'var_dump'

的结果不同

试试这个

$parentNode = simplexml_load_string($xml);
itNode($parentNode);

function itNode($parentNode){
 if($parentNode->children())
  foreach ( $parentNode->children() as $node )
  {
     echo "name ==> " , $node->getName(),'<br>';
     itNode($node);
  }
}