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