2013-10-02 13 views
0

웹 서비스 (야후 날씨)를 쿼리하는 데 문제가 있습니다. 이 멋진 포럼 덕분에 다음과 같은 것을 발견했습니다 : hint. 그럼에도 불구하고 나는 내 가치를 찾을 수 없습니다.PHP XML CDATA 구문 분석

내가

$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를 사용하여 CDATA 부분을 추출하고 항상 비어 있습니다. 이

<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> 

답변

0

OK, 나는 (너무 문제가 논의되었다이 page 덕분에) 문제가 있지만 정규식을 사용하여 해결 :

// 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]; 

건배를

1

문제는 파서가 CDATA 블록 내의 모든 데이터를 무시처럼

설명 보인다. 설명 본문을 다른 DOMDocument에로드해야합니다.

+0

무슨 뜻 $ conditionIcon = $ weatherXmlObject- > xpath ("// item/description"); $ dom = 새 DOMDocument(); $ dom-> loadHTML ($ conditionIcon); $ xml = simplexml_import_dom ($ dom); $ newDoc = 새 DOMDocument(); $ newDoc-> loadHTML ((string) $ xml-> body); $ newXml = simplexml_import_dom ($ newDoc); $ imgSrc = (문자열) $ newXml-> img [ 'src']; echo $ imgSrc; – AntonSack