duan5362 2015-08-28 11:32
浏览 58

无法将元素与从PHP中的URL获取的xml内容分开

I have fetched the contents from a url using CURL which is in the form of XML. But i want to separate the contents in the form of variables which i tried doing using "simplexml_load_string". My contents from the url are:-

 <GetGPSRawDataResponse xmlns="http://example.org/">
    <GetGPSRawDataResult>
    [{"IMIENO":"35xxxxxxxxxxx","Lattitude":24.4286285,"Longitude":73.0507245,"Altitude":0.000,"Speed":0.000,"CTime":"Jul 8 2015 7:24:02:000PM","RecordTime":"Jul 9 2015 8:46AM","DevDistance":0.1000,"CardID":""},{"IMIENO":"35xxxxxxxxxx","Lattitude":24.4286285,"Longitude":73.0507245,"Altitude":0.000,"Speed":0.000,"CTime":"Jul 8 2015 7:19:02:000PM","RecordTime":"Jul 9 2015 8:49AM","DevDistance":0.0950,"CardID":""}]
    </GetGPSRawDataResult></GetGPSRawDataResponse>

I tried fetching the contents from the xml like:-

$abc=get_data($url);//get_data is a function that is working to fetch url contents
//After fetching the above contents from the url.
I did $xml_retrieve=simplexml_load_string($abc);

but the $xml returns nothing. I dont understand why? I want to get the latitude and longitude from the above contents But I dont understand where m i doing wrong if this is the only way

  • 写回答

1条回答 默认 最新

  • dongle7553 2015-08-28 12:20
    关注

    HAving never used simplxml I do not know for sure but it looks like all you have done is load the xml to create the reference to the simplexml object. I think you need to use another method on $xml_retrieve to get the actual data that you want.

    From the manual it states that simplexml_load_string:

    Returns an object of class SimpleXMLElement with properties containing the data held within the xml document.

    I tried playing around with simple_xml but could not get the xpath method to work so reverted to using standard the DOMDocument.

    libxml_use_internal_errors( true);
    $dom = new DOMDocument;
    $dom->validateOnParse=false;
    $dom->standalone=true;
    $dom->preserveWhiteSpace=true;
    $dom->strictErrorChecking=false;
    $dom->recover=true;
    $dom->loadXML( $get_data( $url ) );
    libxml_clear_errors();
    
    /* Get the node in the document you need */
    $col=$dom->getElementsByTagName('GetGPSRawDataResult');
    $node=$col->item(0);
    /* decode response */
    $o=json_decode( $node->nodeValue );
    /*Get first item from array */
    $j=$o[0];
    
    /* do whatever you need with each of the constituent pieces from the json data */
    echo 'IMIENO='.$j->IMIENO.' Lattitude='.$j->Lattitude.' Longitude='.$j->Longitude;
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)