加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP的SimpleXML不保持不同元素类型之间的顺序

发布时间:2020-12-13 21:51:21 所属栏目:PHP教程 来源:网络整理
导读:据我所知,当你在 XML文档树中同一级别有多种类型的元素时,PHP的SimpleXML(包括SimpleXMLElement和SimpleXMLIterator)都不会保持元素的顺序,因为它们彼此相关,只在每个元素. 例如,请考虑以下结构: catalog book titleHarry Potter and the Chamber of Secret
据我所知,当你在 XML文档树中同一级别有多种类型的元素时,PHP的SimpleXML(包括SimpleXMLElement和SimpleXMLIterator)都不会保持元素的顺序,因为它们彼此相关,只在每个元素.

例如,请考虑以下结构:

<catalog>
    <book>
        <title>Harry Potter and the Chamber of Secrets</title>
        <author>J.K. Rowling</author>
    </book>
    <book>
        <title>Great Expectations</title>
        <author>Charles Dickens</author>
    </book>
</catalog>

如果我有这个结构并使用SimpleXMLIterator或SimpleXMLElement来解析它,我最终得到一个类似于下面的数组:

Array (
    [book] => Array (
        [0] => Array (
            [title] => Array (
                [0] => Harry Potter and the Chamber of Secrets
            )
            [author] => Array (
                [0] => J.K. Rowling
            )
        )
        [1] => Array (
            [title] => Array (
                [0] => Great Expectations
            )
            [author] => Array (
                [0] => Charles Dickens
            )
        )
    )
)

这样就好了,因为我只有书本元素,并且它在这些元素中保持正确的顺序.但是,我也说我添加了电影元素:

<catalog>
    <book>
        <title>Harry Potter and the Chamber of Secrets</title>
        <author>J.K. Rowling</author>
    </book>
    <movie>
        <title>The Dark Knight</title>
        <director>Christopher Nolan</director>
    </movie>
    <book>
        <title>Great Expectations</title>
        <author>Charles Dickens</author>
    </book>
    <movie>
        <title>Avatar</title>
        <director>Christopher Nolan</director>
    </movie>
</catalog>

使用SimpleXMLIterator或SimpleXMLElement进行解析将导致以下数组:

Array (
    [book] => Array (
        [0] => Array (
            [title] => Array (
                [0] => Harry Potter and the Chamber of Secrets
            )
            [author] => Array (
                [0] => J.K. Rowling
            )
        )
        [1] => Array (
            [title] => Array (
                [0] => Great Expectations
            )
            [author] => Array (
                [0] => Charles Dickens
            )
        )
    )
    [movie] => Array (
        [0] => Array (
            [title] => Array (
                [0] => The Dark Knight
            )
            [director] => Array (
                [0] => Christopher Nolan
            )
        )
        [1] => Array (
            [title] => Array (
                [0] => Avatar
            )
            [director] => Array (
                [0] => James Cameron
            )
        )
    )
)

因为它以这种方式表示数据,所以我似乎无法分辨XML文件中的书籍和电影的顺序实际上是书籍,电影,书籍,电影.它只是将它们分为两类(尽管它保持每个类别中的顺序).

有没有人知道一个变通方法,或者没有这种行为的不同XML解析器?

解决方法

“如果我…使用SimpleXMLIterator或SimpleXMLElement来解析它,我最终会得到一个数组” – 不,你不会,你会得到一个对象,它在某些方面恰好像一个数组.

该对象的递归转储的输出与迭代它的结果不同.

特别是,运行foreach($some_node-> children()为$child_node)将按照它们在文档中出现的顺序为您提供节点的所有子节点,而不管名称如何,如this live code demo所示.

码:

$xml = <<<EOF
<catalog>
    <book>
        <title>Harry Potter and the Chamber of Secrets</title>
        <author>J.K. Rowling</author>
    </book>
    <movie>
        <title>The Dark Knight</title>
        <director>Christopher Nolan</director>
    </movie>
    <book>
        <title>Great Expectations</title>
        <author>Charles Dickens</author>
    </book>
    <movie>
        <title>Avatar</title>
        <director>Christopher Nolan</director>
    </movie>
</catalog>
EOF;

$sx = simplexml_load_string($xml);
foreach ( $sx->children() as $node )
{
    echo $node->getName(),'<br />';
}

输出:

book
movie
book
movie

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读