2015-09-13 30 views
-4

我知道小小的PHP,但我不是這裏的專家...... - unfortunatley ...所以這個問題需要你的建議:我試圖從一個數據提取一些數據.Osm文件。 -.-你可以在立交橋渦輪跑這裏來演示命令我希望將數據存儲在數據庫 - (或者至少在一個excel文件)SimpleXMLElement :: xpath:從php osx提取數據xml文件

> /* This has been generated by the overpass-turbo wizard. The original 
> search was: 「hospital in rome」 
> */ [out:json][timeout:25]; // fetch area 「」 to search in {{geocodeArea:rome}}->.searchArea; // gather results ( // query part 
> for:rome 「hospital」 node["amenity"="hospital"](area.searchArea); 
> way["amenity"="hospital"](area.searchArea); 
> relation["amenity"="hospital"](area.searchArea);); // print results 
> out body; 
> >; out skel qt; 

所以我有 - XML的量內 - 數據 - 這樣的事情:

「標籤」:{ 「地址:城市」: 「巴塞羅那」, 「地址:housenumber」: 「1」, 「地址:郵編」: 「08022」 , 「addr:street」:「Carrer de Torras i Pujalt」, 「amenity」:「hospital」, 「contact:email」:「[email protected]」, 「聯繫方式:傳真」: 「+34 932124050」, 「聯繫:電話」: 「+34 932122300」, 「聯繫人:網站」: 「http://csf.com.es/」, 「名」: 「CLÍNICA聖家堂」}

所以,問題是這樣的:如何存儲所有這些值,我試圖做一些沒有成功的foreach。當然,我不知道爲什麼我們有一個迭代獲得所有的價值,爲什麼不能一起存儲。

那麼我聽說我們可以在這裏使用SimpleXMLElement :: xpath來獲取所有的值和相應的值。例如:

> $raw = <<<EOF <root> { "type": "node", "id": 583257940, "lat": 
> 41.4134446, "lon": 2.1426945, "tags": { 
>  "amenity": "hospital", 
>  "emergency": "yes", 
>  "name": "Clinica Delfos" } }, { "type": "node", "id": 618312181, "lat": 41.4138593, "lon": 2.1970778, "tags": { 
>  "addr:city": "Barcelona", 
>  "addr:housenumber": "211", 
>  "addr:postcode": "08020", 
>  "addr:street": "Carrer de Fluvià", 
>  "amenity": "hospital", 
>  "health_facility:type": "health_centre", 
>  "medical_system:western": "yes", 
>  "name": "CAP Sant Martí", 
>  "phone": "+93 307 07 66" } }, { "type": "node", "id": 876348001, "lat": 41.3841883, "lon": 2.1952253, "tags": { 
>  "amenity": "hospital", 
>  "emergency": "yes", 
>  "name": "Hospital del Mar", 
>  "website": "http://www.parcdesalutmar.cat/hospitals/hospital-del-mar/index.html", 
>  "wheelchair": "yes" } }, 
> 
> </root> EOF; $xml = simplexml_load_string($raw); 
> 
> foreach($xml->xpath("//way") AS $way){ 
>  $via = $way->xpath("tag[@k='name']/@v")[0]; 
>  foreach($way->nd AS $nd){ 
>   $idnode = $nd["ref"]; 
>   echo $idnode .", ". $via ."<br>"; 
>  } } 

問題:如何在這裏使用的SimpleXMLElement :: XPath的?

XPath的方法,可以幫助這裏:

我需要從上述選擇中的所有數據:

> "tags": { 
>  "addr:city": "Barcelona", 
>  "addr:housenumber": "1", 
>  "addr:postcode": "08022", 
>  "addr:street": "Carrer de Torras i Pujalt", 
>  "amenity": "hospital", 
>  "contact:email": "[email protected]", 
>  "contact:fax": "+34 932124050", 
>  "contact:phone": "+34 932122300", 
>  "contact:website": "http://csf.com.es/", 
>  "name": "Cliníca Sagrada Família" } 

這意味着,我必須選擇上述所有的XML文檔中提及任何地方的元素。

tag [@ k ='name']選擇當前上下文節點的子節點,其中k個屬性值的名稱相同。然後,從那裏,/ @ v返回v屬性。

Finallyi必須getthe以下數據集:

> "tags": { 
>  "addr:city": "Barcelona", 


"addr:housenumber": "1", 
    "addr:postcode": "08022", 
    "addr:street": "Carrer de Torras i Pujalt", 
    "amenity": "hospital", 
    "contact:email": "[email protected]", 
    "contact:fax": "+34 932124050", 
    "contact:phone": "+34 932122300", 
    "contact:website": "http://csf.com.es/", 
    "name": "Cliníca Sagrada Família" } 

順便說一句 - 你可以在立交橋渦輪跑這裏來演示命令

那麼你得到很多結果.... 問題:如何將結果存儲在excel或mysql中?

我真的很喜歡聽到你

提前

回答

1
"tags": { "addr:city": "Barcelona", "addr:housenumber": "1", 
"addr:postcode": "08022", "addr:street": "Carrer de Torras i Pujalt", 
"amenity": "hospital", "contact:email": "[email protected]", 
"contact:fax": "+34 932124050", "contact:phone": "+34 932122300", 
"contact:website": "http://csf.com.es/", "name": "Cliníca Sagrada Família" } 

很多感謝和:

問題:如何使用的SimpleXMLElement :: XPath的嗎?

XPath用於XML。這是文本數據,看起來像JSON結構。雖然XPath 3.1具有處理JSON結構化數據的功能,但PHP不支持它們的XPath處理器。

這意味着我必須在XML文檔的任何位置選擇上述所有元素。

您的示例PHP代碼中只有一個元素<root>。您可以使用xpath /root來選擇它,但它不會對您有所幫助,您仍將擁有非結構化的JSON字符串。

相反,check out json_decode,它意味着解析JSON並將其變成一個PHP變量。該頁面顯示了一些關於如何做到這一點的例子。