2015-08-28 92 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