2014-04-24 28 views
-1

我不知道如何從這個XML代碼得到PHP值的緯度,經度值:從CDATA獲取XML

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://earth.google.com/kml/2.1"> 
<Document> 
<name>OpenCellID Cells</name> 
<description>List of available cells</description> 
<Placemark><name></name><description><![CDATA[lat: <b>3.378199</b><br/>lon: <b>-76.523528</b><br/>mcc: <b>732</b><br/>mnc: <b>123</b><br/>lac: <b>4003</b><br/>cellid: <b>26249364</b><br/>averageSignalStrength: <b>0</b><br/>samples: <b>10</b><br/>changeable: <b>1</b>]]></description><Point><coordinates>-76.523528,3.378199,0</coordinates></Point></Placemark> 
</Document> 
</kml> 

我希望你能幫助我與此有關。謝謝

+0

首先從XML中讀取HTML片段,然後將其加載到單獨的DOM中:http://stackoverflow.com/a/22490106/2265374 – ThW

回答

0

訣竅是先讀出cdata作爲字符串,讓libxml將其包裝成格式良好的html,然後解析出包含數據的節點中的值。

請注意,這個工作,但假設LON和LAT在第一節點始終都在CDATA

// the xml as a variable 
$xml = '<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://earth.google.com/kml/2.1"> 
<Document> 
<name>OpenCellID Cells</name> 
<description>List of available cells</description> 
<Placemark><name></name><description><![CDATA[lat: <b>3.378199</b><br/>lon:  <b>-76.523528</b><br/>mcc: <b>732</b><br/>mnc: <b>123</b><br/>lac: <b>4003</b><br/>cellid: <b>26249364</b><br/>averageSignalStrength: <b>0</b><br/>samples: <b>10</b><br/>changeable: <b>1</b>]]></description><Point><coordinates>-76.523528,3.378199,0</coordinates></Point></Placemark> 
</Document> 
</kml>'; 

// read into dom 
$domdoc = new DOMDocument(); 

$domdoc->loadXML($xml); 

// the cdata as a string 
$cdata = $docdom->getElementsByTagName('Placemark')->item(0)->getElementsByTagName('description')->item(0)->nodeValue; 

// a dom object for the cdata 
$htmldom = new DOMDocument(); 

// wrap in html and parse 
$htmldom->loadHTML($cdata); 

// get the <b> nodes 
$bnodes = $htmldom->getElementsByTagName('b'); 

// your data :) 
$lon = $bnodes->item(0)->nodeValue; 
$lat = $bnodes->item(1)->nodeValue; 

最後並非最不重要的,這是爲了說明的loadXML和loadHTML有何區別,以及如何使用。至於googleeart kml,我相信這是一種更爲標準的解析方式...