【问题标题】:PHP - XML to JSON format changes when number of children is just onePHP - 当孩子的数量只有一个时,XML 到 JSON 格式会发生变化
【发布时间】:2015-09-07 13:58:57
【问题描述】:

我正在尝试使用@hakre 在此处建议的JsonSerializable& iterator_to_array() 将具有命名空间、属性的 XML 转换为数组: https://hakre.wordpress.com/2013/07/10/simplexml-and-json-encode-in-php-part-iii-and-end/

我面临的问题是 XML 中的一个节点只有一个子节点。 问题描述如下:

<node1 attr1="val1" attr2="val2>
    <node2 attr3="val3" attr4="val4" />
    <node2 attr3="val3" attr4="val4" />
    <node2 attr3="val3" attr4="val4" />
</node1>

所以数组是

[node1] => Array
                    (
                        [@attributes] => Array
                            (
                                [attr1] => val1
                                [attr2] => val2
                            )
                        [node2] => Array
                            (
                                [0] => Array
                                    (
                                        [@attributes] => Array
                                               (
                                                   [attr3] => val3
                                                   [attr4] => val4
                                               )
                                    )

                                [1] => Array
                                    (
                                        [@attributes] => Array
                                               (
                                                   [attr3] => val3
                                                   [attr4] => val4
                                               )
                                    )

                                [2] => Array
                                    (
                                        [@attributes] => Array
                                               (
                                                   [attr3] => val3
                                                   [attr4] => val4
                                               )
                                    )
                            )
                    )

所以我会使用类似的东西:

foreach($array['node1']['node1'] AS $node){
    print_r($node['@attributes']);
}

但有时……

<node1 attr1="val1" attr2="val2>
    <node2 attr3="val3" attr4="val4" />
</node1>

然后数组是这样的:

[node1] => Array
                    (
                        [@attributes] => Array
                            (
                                [attr1] => val1
                                [attr2] => val2
                            )
                        [node2] => Array
                            (
                                  [@attributes] => Array
                                        (
                                             [attr3] => val3
                                             [attr4] => val4
                                        )
                            )
                    )

在这种情况下,foreach 会失败,因为 @attributesnode2 的直接子代,这与第一种情况 node2[0]['@attributes'] 不同。

XML 是一个 Web 服务响应,整个信息都在属性中。一个特定的节点可能只有一个或多个没有预期的孩子。

即使只有一个像下面这样的子节点,我如何确保我有一个 0th 子节点?

所需的 O/P:

[node1] => Array
                    (
                        [@attributes] => Array
                            (
                                [attr1] => val1
                                [attr2] => val2
                            )
                        [node2] => Array
                            (
                                  [0] => Array
                                    (
                                        [@attributes] => Array
                                               (
                                                   [attr3] => val3
                                                   [attr4] => val4
                                               )
                                    )
                            )
                    )

注意:我设计的一个简单的解决方法

if(isset($array['node1']['node2']['@attributes']){
    print_r($array['node1']['node2']['@attributes']);
}
else{
    foreach($array['node1']['node1'] AS $node){
        print_r($node['@attributes']);
    }
}

但是 XML 响应很容易有 7000 行,并且在不同级别中存在超过 20 个不同的节点。我不想为每个节点明确地使用这个条件。

PS:我已经研究过这个问题,但我想要相反。即使有一个孩子也添加第 0 个项目 PHP convert XML to JSON group when there is one child

【问题讨论】:

    标签: php json xml xml-parsing


    【解决方案1】:

    我目前正在处理jsonSerialize 的实现,它总是产生数组,即使 XML 源只包含一个孩子。 https://github.com/vitr/SimpleXMLExtended
    我提供了您的 XML 示例,它产生了(可能是偶然的)所需的 o/p,看看
    https://eval.in/385372
    如果它真的解决了问题,我可能会将您的案例包含在我的测试套件中。如果没有,您可以克隆 repo 并进行进一步的调整。无论如何,也许它会为您指明正确的方向。

    【讨论】:

    • 它适用于有问题的 XML,但我用实际的 XML 替换它,然后我得到了空数组。 :/ 你还需要什么信息?
    • 顺便说一句,我们正在查看的 XML 响应是 TravelPort uAPI for Flights
    • @eNeMetcH 很抱歉,我正在处理的是 SOAP 而不是纯 XML,请尝试类似 stackoverflow.com/questions/21777075/…
    • 如果我删除 SOAP 信封和正文标签,它是纯 XML 吗?
    • 我们试试看,simplexml 好像不能处理soap 因为冒号:
    猜你喜欢
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多