使用来自URL php的属性提取XML数据

时间:2014-11-27 09:54:20

标签: php xml curl simplexml

我有一个响应XML数据的网址 像这样

<Account account="ihs" timezone="GMT+05:30">
<Description>The Indian Heights School</Description>
<Device id="09647">
<Description>DL1PD0228</Description>
<EventData device="09647">
<Timestamp epoch="1416968997">2014/11/26 07:59:57 GMT+05:30</Timestamp>
<StatusCode code="0xF401">Ignition_On</StatusCode>
<GPSPoint>28.56262,77.05264</GPSPoint>
<Speed units="km/h">0.0</Speed>
<Odometer units="Km">4390.1</Odometer>
<Geozone index="0">tihs</Geozone>
<Address>The Indian Heights School</Address>
</EventData>
</Device>
<Device id="8786">
<Description>DL1VC1750</Description>
<EventData device="8786">
<Timestamp epoch="1416989072">2014/11/26 13:34:32 GMT+05:30</Timestamp>
<StatusCode code="0xF020">Location</StatusCode>
<GPSPoint>28.56234,77.05284</GPSPoint>
<Speed units="km/h">0.0</Speed>
<Odometer units="Km">6154.6</Odometer>
<Geozone index="0">tihs</Geozone>
<Address>The Indian Heights School</Address>
</EventData>
</Device>

我想使用php从xml中提取所有<Description> tage 请帮帮我

我也在使用curl, simplexmlloadfile, simplexmlloadstring 我这样做

 $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
print_r($xml)

我得到了

     SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [account] => ihs
            [timezone] => GMT+05:30
        )

    [Description] => SimpleXMLElement Object
        (
        )

    [Device] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 09647
                        )

                    [Description] => SimpleXMLElement Object
                        (
                        )

                    [EventData] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [device] => 09647
                                )

                            [Timestamp] => 2014/11/26 07:59:57 GMT+05:30
                            [StatusCode] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [code] => 0xF401
                                        )

                                )

                            [GPSPoint] => 28.56262,77.05264
                            [Speed] => 0.0
                            [Odometer] => 4390.1
                            [Geozone] => tihs
                            [Address] => SimpleXMLElement Object
                                (
                                )

                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 8786
                        )

                    [Description] => SimpleXMLElement Object
                        (
                        )

                    [EventData] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [device] => 8786
                                )

                            [Timestamp] => 2014/11/26 14:45:33 GMT+05:30
                            [StatusCode] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [code] => 0xF113
                                        )

                                )

                            [GPSPoint] => 28.61029,76.98159
                            [Speed] => 0.0
                            [Odometer] => 6155.0
                        )

                )

在描述前面我注意到并且不知道如何提取它

1 个答案:

答案 0 :(得分:0)

你需要保持敏感:

echo $xml->Description;

也是一个foreach:

foreach($xml->device as $device) {
    echo $device->Description;
}