【问题标题】:Parsing a foursquare KML venue feed with simpleXML使用 simpleXML 解析四方 KML 场地提要
【发布时间】:2011-11-06 18:06:30
【问题描述】:

我正在尝试使用 simpleXML 解析来自foursqare 的KML 场地提要,但我无法获得嵌套在描述中的场地网址。看起来 simpleXML 剥离了它。

详细说明:

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);
有什么想法可以保留description 中的 html 吗?

【问题讨论】:

  • 这很困难,因为 KML 是错误的(至少我会说) - &lt;description&gt; 的内容应该是 CDATA 如果它可以包含 HTML。我知道这并没有真正的帮助,但可能值得向提要提供者提出这一点,因为他们不这样做会使消费者的生活变得更加困难......
  • 感谢您的建议。我不妨这样做,但它不会很快为我提供解决方案。 :\
  • 在将其传递给 simplexml_load_file() 之前,如何在 标记中自己添加 CDATA 包装器?
  • 是的,因为我不能直接用 simpleXML 处理这个问题,所以最终需要某种预处理。

标签: php simplexml kml foursquare


【解决方案1】:

他们是对的。这是无效的 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];
    }
}

希望对您有所帮助...

【讨论】:

  • 谢谢@Homer6 几天没关注stackoverflow,错过了。我不得不在我的项目中使用一种解决方法,所以我满足了这个需求,但我会将它存储起来以备将来使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多