我有一个XML文件,我想要获取其中的节点属性值,它在节点通常情况下有效工作,但是节点名称类似于某些情况:它没有给我任何结果,只是空。 XML文件:从XML文件中的节点获取属性值
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Sunnyvale, CA</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
<description>Yahoo! Weather for Sunnyvale, CA</description>
<language>en-us</language>
<lastBuildDate>Fri, 18 Dec 2009 9:38 am PST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Sunnyvale" region="CA" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
</channel>
</rss>
Java代码:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//rss/@version");
Object result = expr.evaluate(doc, XPathConstants.STRING);
System.out.println(result);
以前的Java代码有效地工作,但与 //rss/channel/yweather:location/@city
更换//rss/@version
当它返回我空。
了解更多关于它你没有在任何地方注册该名称空间。再次阅读最后一个问题中的评论,完全相同的问题。 –
@HovercraftFullOfEels所以,我没有什么可抱怨的:) – dirkk