我的XML結構是這樣的:解析XML使用dom4j的
<rss>
<channel>
<yweather:location city="Paris" region="" country="France"/>
<yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
<yweather:wind chill="-1" direction="40" speed="11.27"/>
<yweather:atmosphere humidity="87" visibility="9.99" pressure="1015.92" rising="0"/>
<yweather:astronomy sunrise="8:30 am" sunset="4:54 pm"/>
</channel>
</rss>
,當我試圖使用dom4j的
SAXReader xmlReader = createXmlReader();
Document doc = null;
doc = xmlReader.read(inputStream);//inputStream is input of function
log.info(doc.valueOf("/rss/channel/yweather:location/@city"));
private SAXReader createXmlReader() {
Map<String,String> uris = new HashMap<String,String>();
uris.put("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
uris.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
DocumentFactory factory = new DocumentFactory();
factory.setXPathNamespaceURIs(uris);
SAXReader xmlReader = new SAXReader();
xmlReader.setDocumentFactory(factory);
return xmlReader;
}
解析它,但我在cmd中什麼都沒有,但是當我打印doc.asXML( ),我的XML結構打印正確!
這是我從這個XML的網址: http://weather.yahooapis.com/forecastrss?w=615702&u = c – D3GAN
因此,在原始XML中有一個名稱空間聲明,但是您仍然不會將XPath的前綴'yweather:'設置爲可用。當你解決這個問題會發生什麼? –
XML中有兩個命名空間(正如您可以看到的,如果您點擊此鏈接weather.yahooapis.com/forecastrss?w=615702&u=c) 我將它們兩個添加爲: uris.put(「yweather」,「http ://xml.weather.yahoo.com/ns/rss/1.0「); uris.put(「geo」,「http://www.w3.org/2003/01/geo/wgs84_pos#」); – D3GAN