简体   繁体   English

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

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

I am getting XML response from API but I am facing a little problem. 我从API获得XML响应,但遇到了一个小问题。 I can not get name an attribute from XML response. 我无法从XML响应中获取属性name

I am sharing image XML response data. 我正在共享图像XML响应数据。 在此处输入图片说明

Here is my code which I am trying to get name attribute but I can not getting so Please help me. 这是我尝试获取name属性的代码,但无法获取,请帮助我。

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

when I execute my above code it is not displayed name attributes like order_id, origin,destination .it is displayed 0,1,2 keys rather than displayed name attributes. 当我执行我上面的代码不显示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
                                )

                        )

                 )
)
)

If you read up about SimpleXML, then you'll find that doing a print_r doesn't give all of the data. 如果您阅读有关SimpleXML的文章,那么您会发现执行print_r并不能提供所有数据。 If you wanted the name attribute of the field elements, then you can do something like... 如果您想要field元素的name属性,则可以执行以下操作:

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

The foreach will go take the loaded XML and then pick out the first object element (the [0] will pick the first) and within that each of the field elements. foreach将获取已加载的XML,然后选择第一个object元素([0]将选择第一个object元素),并在其中每个field元素。 The echo line uses the array notation to fetch the name attribute. echo行使用数组表示法来获取name属性。

If you wanted a specific object element,then you can use XPath to find this object and do similar to the above and print each one out... 如果需要特定的object元素,则可以使用XPath查找该对象,并执行与上面类似的操作,然后将每个对象打印出来。

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

The XPath above, picks out the <object pk="2"...> element. 上面的XPath选择<object pk="2"...>元素。 Note that ->xpath() returns an array of matching nodes, so again here I'm just using the first one ([0]). 请注意, ->xpath()返回匹配节点的数组,因此在这里我仅使用第一个([0])。

To help check the elements you've selected and when print_r isn't much help, you can use asXML() to output the XML of a node... 为了帮助检查选定的元素,并且在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