PHP XML无法解析CDATA

PHP XML无法解析CDATA,php,xml,Php,Xml,我正在将来自国家数据浮标中心的RSS提要解析为PHP。我无法解析标记为CDATA的描述。最终目标是具有描述项变量,如位置、风向、风速等。。我不确定如何打破这一点,并省略标记 以下是提要的一个片段: <item> <pubDate>Thu, 08 Sep 2011 17:59:39 UT</pubDate> <title>Station SFXC1 - SAN FRANCISCO BAY RESERVE, CA</title>

我正在将来自国家数据浮标中心的RSS提要解析为PHP。我无法解析标记为CDATA的描述。最终目标是具有描述项变量,如位置、风向、风速等。。我不确定如何打破这一点,并省略标记

以下是提要的一个片段:

<item>
  <pubDate>Thu, 08 Sep 2011 17:59:39 UT</pubDate>
  <title>Station SFXC1 - SAN FRANCISCO BAY RESERVE, CA</title>
  <description><![CDATA[
    <strong>September 8, 2011 9:45 am PDT</strong><br />
    <strong>Location:</strong> 38.223N 122.026W or 77 nautical miles S of search location of 39.5N 122.1W.<br />
    <strong>Wind Direction:</strong> W (270&#176;)<br />
    <strong>Wind Speed:</strong> 11 knots<br />
    <strong>Atmospheric Pressure:</strong> 30.03 in (1017.0 mb)<br />
    <strong>Air Temperature:</strong> 62&#176;F (16.9&#176;C)<br />
    <strong>Dew Point:</strong> 50&#176;F (10.2&#176;C)<br />
  ]]></description>

  <link>http://www.ndbc.noaa.gov/station_page.php?station=sfxc1</link>
  <guid>http://www.ndbc.noaa.gov/station_page.php?station=sfxc1&amp;ts=1315500300</guid>
  <georss:point>38.223 -122.026</georss:point>
</item>

2011年9月8日星期四17:59:39 UT
CA旧金山湾保护区SFXC1站
2011年9月8日太平洋时间上午9:45
位置:38.223N 122.026W或搜索位置南纬77海里39.5N 122.1W。
风向:W(270°;)
风速:11节
大气压力:30.03英寸(1017.0毫巴)
气温:62°;F(16.9°;C)
露点:50和176;F(10.2°;C)
]]> http://www.ndbc.noaa.gov/station_page.php?station=sfxc1 http://www.ndbc.noaa.gov/station_page.php?station=sfxc1&ts=1315500300 38.223 -122.026
以下是PHP:

$feed_url = "http://www.ndbc.noaa.gov/rss/ndbc_obs_search.php?lat=39.5&lon=-122.1&radius=400";
$xmlString = file_get_contents($feed_url); 
$xmlString = str_replace('georss:point','point',$xmlString);  
$xml = new SimpleXMLElement($xmlString); 
$items = $xml->xpath('channel/item');
$closeItems = array(); 
$new_array = array(); 
foreach($items as &$item)  
{ 
echo "<br>";
$item_title = $item->title;
$item_title = mb_convert_case($item_title, MB_CASE_UPPER, "UTF-8");
list($lat, $lng) = explode(' ',trim($item->point));
   echo $item_title;
echo "<br>";     
echo $lat;
echo "<br>";
echo $lng;
echo "<br>";
echo $item->description;
echo "<br>";
echo $item->pubDate;
echo "<br>";
} 
$feed\u url=”http://www.ndbc.noaa.gov/rss/ndbc_obs_search.php?lat=39.5&lon=-122.1和半径=400”;
$xmlString=file\u get\u contents($feed\u url);
$xmlString=str_replace('georss:point','point',$xmlString);
$xml=新的simplexmlement($xmlString);
$items=$xml->xpath('channel/item');
$closeItems=array();
$new_array=array();
foreach($items as&$item)
{ 
回声“
”; $item\u title=$item->title; $item_title=mb_convert_case($item_title,mb_case_UPPER,“UTF-8”); 列表($lat,$lng)=爆炸('',修剪($item->点)); echo$item_title; 回声“
”; echo$lat; 回声“
”; echo$lng; 回声“
”; echo$item->description; 回声“
”; echo$item->pubDate; 回声“
”; }
在这种情况下,不要只呼应期望值,当值为空时放弃,然后哭到如此地步(只是在开哭泣部分的玩笑)

使用PHP的或,看看你真正得到了什么。它是空的吗?是空字符串吗?您是否需要进入其他SimpleXMLElement对象


这不仅会使你的问题更具信息性,更有可能得到回答,而且你最终可能会自己解决问题(然后在这里为将来偶然发现问题的其他人发布答案)。

重写我的解决方案,使其真正正确:
$feed_url = "http://www.ndbc.noaa.gov/rss/ndbc_obs_search.php?lat=39.5&lon=-122.1&radius=400";
$xmlString = file_get_contents($feed_url); 
$xmlString = str_replace('georss:point','point',$xmlString);  
$xml = new SimpleXMLElement($xmlString); 
$items = $xml->xpath('channel/item');
foreach($items as $item) { 

$item_title = mb_convert_case($item->title, MB_CASE_UPPER, "UTF-8");
$description = mb_convert_case(str_replace('        ', '', trim(html_entity_decode(strip_tags($item->description)))), MB_CASE_UPPER, "UTF-8");

list($lat, $lng) = explode(' ',trim($item->point));

echo $item_title . PHP_EOL . $lat . ' x ' . $lng . PHP_EOL . 'published: ' . $item->pubDate . PHP_EOL . 'Description: ' . PHP_EOL . $description . PHP_EOL . PHP_EOL;
}

我用CDATA删除了标签,解码了html实体,并删除了讨厌的空白。正则表达式在删除空白方面可能更好。

Namspaces并不难。给你的大脑一些食物,而不是走最省力的路线。学习新知识。这是一个cdata问题,通过对LIBXML\u NOCDATA的澄清解决了。因此,请告诉我这是一个怎样的名称空间问题?请参考链接[[1]: