2

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!

D3GAN
  • 642
  • 10
  • 26

2 Answers2

0

You have an undeclared namespace prefix, both in the XML and in your XPath.

Add an appropriate xmlns:yweather declaration to your XML, and use setNamespaceURIs to make it available to your XPath.

Paul Butcher
  • 6,902
  • 28
  • 39
  • This is the URL that I get this XML from: http://weather.yahooapis.com/forecastrss?w=615702&u=c – D3GAN Dec 08 '12 at 10:42
  • So, there is an namespace declaration in the original XML, but you still don't make the prefix `yweather:` available to your XPath. What happens when you fix that? – Paul Butcher Dec 08 '12 at 10:56
  • There is two namespaces in XML (as you can see if you click on this link weather.yahooapis.com/forecastrss?w=615702&u=c) I add both of them : 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 Dec 08 '12 at 11:01
  • And did that fix it, or are you still struggling? – Paul Butcher Dec 08 '12 at 17:55
  • No Im still struggling!:( I changed my Weather API to this http://free.worldweatheronline.com/feed/weather.ashx?key=c3a35b2b0d124616120912&q=tehran%20iran&num_of_days=4&format=xml but still I can't parse it with dom4j – D3GAN Dec 09 '12 at 14:22
0

@D3GAN Everything you wrote is fine. No issue with the code. I just tried it .The problem is you DONT have all the necessary libraries in your code .

You should add jaxen library to your class path you are probably getting a NoClassdefFoundError. The original dom4j distribution contains jaxen.jar (in dependancies folder).

I made the same mistake. I just added dom4j and the code didnt work . I was getting document empty. But when I added jaxen dependancy it started working fine.

If you dont know how to install JAR files you can go here : http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29

vinzzz
  • 2,084
  • 2
  • 14
  • 23