繁体   English   中英

我没有从php的xml响应中获取属性名称?

[英]I am not getting attribute name from xml response in php?

我从API获得XML响应,但遇到了一个小问题。 我无法从XML响应中获取属性name

我正在共享图像XML响应数据。 在此处输入图片说明

这是我尝试获取name属性的代码,但无法获取,请帮助我。

$xml = new SimpleXMLElement($response);
    echo "<pre>"; print_r($xml);die();

当我执行我上面的代码不显示name属性,如order_id, origin,destination 。它显示,而0,1,2键不是显示name属性。

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 1.0
        )

    [object] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [pk] => 1
                    [model] => awb
                )

            [field] => Array
                (
                    [0] => 899594723
                    [1] => 800000041
                    [2] => 1.13
                    [3] => JHANSI - JHA
                    [4] => DELHI - DLO
                    [5] => DELHI - DLO
                    [6] => DLO
                    [7] => KAMASHYA ONLINE SHOPPING - 331428
                    [8] => R G
                    [9] => 28-Jan-2018
                    [10] => Delivered / Closed
                    [11] => Delivered
                    [12] => 999 - Delivered
                    [13] => Delivered
                    [14] => 999
                    [15] => Self:R G: Android
                    [16] => 0.0000000
                    [17] => 0.0000000
                    [18] => 01-Feb-2018
                    [19] => 01-Feb-2018
                    [20] => 01-Feb-2018 12:44
                    [21] => 2018-02-01 12:43:00
                    [22] => None
                    [23] => 0
                    [24] => 2018-02-01 12:44:20
                    [25] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => rts_system_delivery_status
                                )

                        )

                    [26] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => rts_reason_code_number
                                )

                        )

                    [27] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => rts_last_update
                                )

                        )

                    [28] => 110019
                    [29] => DELHI
                    [30] => New Delhi
                    [31] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => delivery_pod_image
                                )

                        )

                    [32] => http://api3.ecomexpress.in//static/lastmile//sign/2018/2/1/sign_899594723_2018020112441517469260.png
                    [33] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => rev_pickup_signature
                                )

                        )

                    [34] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => rev_pickup_packed_image
                                )

                        )

                    [35] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => CharField
                                    [name] => rev_pickup_open_image
                                )

                        )

                 )
)
)

如果您阅读有关SimpleXML的文章,那么您会发现执行print_r并不能提供所有数据。 如果您想要field元素的name属性,则可以执行以下操作:

$xml = new SimpleXMLElement($response);
foreach ( $xml->object[0]->field as $field )   {
    echo $field['name'].PHP_EOL;
}

foreach将获取已加载的XML,然后选择第一个object元素([0]将选择第一个object元素),并在其中每个field元素。 echo行使用数组表示法来获取name属性。

如果需要特定的object元素,则可以使用XPath查找该对象,并执行与上面类似的操作,然后将每个对象打印出来。

$objectRec = $xml->xpath('//object[@pk="2"]')[0];
foreach ( $objectRec->field as $field )   {
    echo $field['name'].PHP_EOL;
}

上面的XPath选择<object pk="2"...>元素。 请注意, ->xpath()返回匹配节点的数组,因此在这里我仅使用第一个([0])。

为了帮助检查选定的元素,并且在print_r帮助不大的情况下,可以使用asXML()输出节点的XML。

$objectRec = $xml->xpath('//object[@pk="1"]')[0];
echo $objectRec->asXML();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM