名称空间元素的属性值

问题描述:

我正在为我的学校网站开发当前天气和天气预报的天气预报。 为此,我将使用Yahoos RSS天气预报。 在这个XML文件中有一些值存储在属性中。 我想用PHP来解决这些问题。 文件可以在这里找到: http://weather.yahooapis.com/forecastrss?w=12602818&u=c名称空间元素的属性值

的XML文件指出如下因素线:

<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"> 

我想走出值如下:

<yweather:condition text="Partly Cloudy" code="30" temp="22" date="Wed, 12 Jun 2013 4:20 pm CEST"/> 

我会,例如,喜欢从这个yweather:条件接收'temp'。 任何人都可以指示我如何用PHP来完成这项工作吗?

+0

使用XML解析器 - 有许多的PHP – verbumSapienti

+0

请你的问题(使用编辑)内发布XML的指导性和有效部分。谢谢! – michi

+0

请使用搜索。如果我们还没有特别针对雅虎天气RSS做出回答,那么至少可以通过xml-namespaces回答。查看[解析雅虎天气RSS提要获取名称空间城市元素](http://*.com/questions/14095012/parsing-yahoo-weather-rss-feed-to-get-namespaced-city-element?rq=1)以及右边相关栏目中的其他人 - > – hakre

<?php 
$doc = new DOMDocument(); 
$doc->load('http://weather.yahooapis.com/forecastrss?w=12602818&u=c'); 
$channel = $doc->getElementsByTagName("channel"); 
foreach($channel as $chnl){ 
    $item = $chnl->getElementsByTagName("item"); 
    foreach($item as $itemgotten){ 
     $curtemp = $itemgotten->getElementsByTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0","condition")->item(0)->getAttribute("temp"); 
     echo $curtemp; 
    } 
} 
?> 

修复了这个问题! 答案在这里找到: How to get the tag "<yweather:condition>" from Yahoo Weather RSS in PHP?