我的 XML 结构是这样的:
<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 结构打印正确!