2015-08-28 50 views
0

我已经使用XML形式的CURL从URL中获取内容。但我想分开变量的形式,我尝试使用“simplexml_load_string”的内容。 我从URL的内容是: -无法从PHP中从URL中获取的xml内容中分离元素

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

我试图从XML获取的内容,如: -

$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); 

但是$ XML没有返回。我不明白为什么?我想从上述内容中获取经纬度但是我不明白哪里做错了如果这是唯一的方式

回答

0

从未使用过simplxml我不确定,但它看起来像所有你做的是负载xml创建对simplexml对象的引用。我想你需要在$xml_retrieve上使用另一种方法来获得你想要的实际数据。

从手动它指出simplexml_load_string:

返回与含有 xml文档内所保持的数据的属性类的SimpleXMLElement的对象。

我试着玩弄simple_xml,但无法使xpath方法工作,因此恢复使用标准的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; 
+0

它给出错误'class-DOM Document not found' – infinity26

+0

?检查你的系统中的手册(php) - 看起来很奇怪DOMDocument不在那里,因为它是PHP核心功能的一部分。 – RamRaider