parsing xml using dom4j

Posted by D3GAN on Stack Overflow See other posts from Stack Overflow or by D3GAN
Published on 2012-12-08T10:21:20Z Indexed on 2012/12/08 11:06 UTC
Read the original article Hit count: 260

Filed under:
|
|

My XML structure is like this:

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

when I tried to parse it using 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;
}

But I got nothing in cmd but when I print doc.asXML(), my XML structure print correctly!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about dom4j