简体   繁体   中英

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. I can not get name an attribute from XML response.

I am sharing image XML response data. 在此处输入图片说明

Here is my code which I am trying to get name attribute but I can not getting so Please help me.

$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.

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. If you wanted the name attribute of the field elements, then you can do something like...

$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. The echo line uses the array notation to fetch the name attribute.

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...

$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. Note that ->xpath() returns an array of matching nodes, so again here I'm just using the first one ([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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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