douba2011 2013-10-02 12:36
浏览 51
已采纳

PHP XML CDATA解析

I have a problem with querying a web service (Yahoo weather). Thanks to this cool forum, I found the following hint. Nevertheless, I am not able to retrieve my value.

I am extracting the CDATA part by using

$conditionIcon = $weatherXmlObject->xpath("//item/description");
$dom = new DOMDocument();
$dom->loadHTML($conditionIcon); // or you can use loadXML
$xml = simplexml_import_dom($dom);
$imgSrc = (string)$xml->body->img['src'];
echo $imgSrc;

$imgSrc is always empty.

Description looks like this

<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 50 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 62 Low: 49<br />
Sat - Partly Cloudy. High: 65 Low: 49<br />
<br />
<a    href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
  • 写回答

2条回答 默认 最新

  • dovt85093 2013-10-04 14:36
    关注

    OK, I solved the issue but using a regex (thanks to this page where the issue was discussed, too):

    // retrieve link to condition image - in description element
    $xml = simplexml_load_string($weather_feed, 'SimpleXMLElement', LIBXML_NOCDATA); 
    $description = $xml->channel->item->description;
    //preg match regular expression to extract weather icon
    $imgpattern = '/src="(.*?)"/i';
    preg_match($imgpattern, $description, $matches);
    $aExport['img_url'] = $matches[1];
    

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度