2011-11-06 82 views
2

我想用simpleXML解析來自foursqare的KML場地飼料,但是我無法獲取嵌套在desctription場地的網址。它看起來像simpleXML剝離它。用simpleXML解析foursquare KML場地飼料

詳細:

的foursqare KML飼料看起來是這樣的:

<kml> 
<Folder> 
    <name>foursquare checkin history for X</name> 
    <description>foursquare checkin history for X</description> 
    <Placemark> 
     <name>somevenuename</name> 
     <description>@<a href="/v/somevenueurl">somevenuename</a>- a foursqareshout!</description> 
     <updated>Wed, 02 Nov 11 17:00:05 +0000</updated> 
     <published>Wed, 02 Nov 11 17:00:05 +0000</published> 
     <visibility>1</visibility> 
     <Point> 
     <extrude>1</extrude> 
     <altitudeMode>relativeToGround</altitudeMode> 
     <coordinates>xx.xxxxxx,yy.yyyyyy</coordinates> 
     </Point> 
    </Placemark> 
    etc ... 

我對SimpleXML的電話...嗯,很簡單:
$venue_items = simplexml_load_file($venue_kml_file);
任何想法誰保存HTML在description

+0

相當困難這一點,因爲KML是錯誤(我會說,至少) - 的內容應該是CDATA,如果它可以包含HTML的話。我知道這並沒有什麼幫助,但可能值得將它與飼料提供商聯繫起來,因爲他們通過不這樣做讓消費者生活變得更加困難...... – DaveRandom

+0

感謝您的建議。我不妨做到這一點,但它不會很快爲我提供解決方案。 :\ – nikan

+0

在將它傳遞給simplexml_load_file()之前,如何在標記中自己添加CDATA包裝? –

回答

2

他們是對的。這是無效的XML。不過,我用正則表達式爲你寫了一個解決方法。這可能有點哈克,但你只能讓因與你已經給什麼,所以在這裏它是:

$xml_string = <<<XML_STRING 
<kml> 
<Folder> 
    <name>foursquare checkin history for X</name> 
    <description>foursquare checkin history for X</description> 
    <Placemark> 
     <name>somevenuename</name> 
     <description>@<a href="/v/somevenueurl">somevenuename</a>- a foursqareshout!</description> 
     <updated>Wed, 02 Nov 11 17:00:05 +0000</updated> 
     <published>Wed, 02 Nov 11 17:00:05 +0000</published> 
     <visibility>1</visibility> 
     <Point> 
     <extrude>1</extrude> 
     <altitudeMode>relativeToGround</altitudeMode> 
     <coordinates>xx.xxxxxx,yy.yyyyyy</coordinates> 
     </Point> 
    </Placemark> 
</Folder> 
<Folder> 
    <name>foursquare checkin history for X</name> 
    <description>foursquare checkin history for X</description> 
    <Placemark> 
     <name>somevenuename</name> 
     <description>@<a href="/v/somevenueurl222">somevenuename</a>- a foursqareshout!</description> 
     <updated>Wed, 02 Nov 11 17:00:05 +0000</updated> 
     <published>Wed, 02 Nov 11 17:00:05 +0000</published> 
     <visibility>1</visibility> 
     <Point> 
     <extrude>1</extrude> 
     <altitudeMode>relativeToGround</altitudeMode> 
     <coordinates>xx.xxxxxx,yy.yyyyyy</coordinates> 
     </Point> 
    </Placemark> 
</Folder> 
</kml>  

XML_STRING; 


preg_match_all('%<Placemark>(.*?)</Placemark>%s', $xml_string, $placemarks, PREG_SET_ORDER); 
for($x = 0; $x < sizeof($placemarks); $x++){ 
    preg_match_all('%<description>(.*?)</description>%s', $placemarks[$x][1], $descriptions, PREG_SET_ORDER); 
    for($y = 0; $y < sizeof($descriptions); $y++){ 
     echo $descriptions[$y][1]; 
    } 
} 

希望幫助...

+0

謝謝@ Homer6我幾天沒有注意到stackoverflow並且錯過了這個。我必須在我的項目中使用解決方法,所以我通過了這個需求,但我將它存儲起來以備將來使用。 – nikan

+1

我很好奇......是否解決了您的問題? – Homer6