java - what is wrong with this SAX parsing and Xpath code? -


working through maven example, book quite few years ago, is, assume, retrieving right versions repositories, i'm wondering going wrong unit testing in first example.

we have xml file, start thereof:

<?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 - new york, ny</title>  <link>http://us.rd.yahoo.com/dailynews/rss/weather/new_york__ny/*http://weather.yahoo.com/forecast/10002_f.html</link>  <description>yahoo! weather new york, ny</description>  <language>en-us</language>  <lastbuilddate>sat, 10 nov 2007 8:51 pm edt</lastbuilddate>  <ttl>60</ttl>   <!--************ line we're interested in ************ -->  <yweather:location city="new york" region="ny" country="us" />   <yweather:units temperature="f" distance="mi" pressure="in" speed="mph" /> 

and xml file being read in via inputstream , parsed:

    weather weather = new weather();     log.info("creating xml reader");     saxreader xmlreader = createxmlreader();     document doc = xmlreader.read(inputstream);      // proves we're getting text ok     // string xml_text_content = doc.getstringvalue();     // log.info( "=== xml text content: |" + xml_text_content + "|" );      log.info("parsing xml response");     weather.setcity(doc.valueof("/rss/channel/y:location/@city"));     string city_str = doc.valueof("/rss/channel/y:location/@city");     // should "new york"... i'm getting empty string     log.info( "=== doc value of city: |" + city_str + "|" ); 

... know little rss , xpath, sorry. seems knowledgeable might see problem!

ps tried changing "yweather:location" "y:location" in xml file, , "y:location" "yweather:location" in code... both threw exceptions. latter case:

528 info yahooparser - parsing xml response tests run: 1, failures: 0, errors: 1, skipped: 0, time elapsed: 0.096 sec <<< failure! testparser(org.sonatype.mavenbook.custom.weather.yahoo.yahooparsertest) time el apsed: 0.094 sec <<< error!
org.dom4j.xpathexception: exception occurred evaluting xpath: /rss/channel/yweather:location/@city. exception: xpath expression uses unbound namespace prefix yweather
@ org.dom4j.xpath.defaultxpath.handlejaxenexception(defaultxpath.java:374) @ org.dom4j.xpath.defaultxpath.valueof(defaultxpath.java:185) @ org.dom4j.tree.abstractnode.valueof(abstractnode.java:191) @ org.sonatype.mavenbook.custom.weather.yahooparser.parse(yahooparser.j ava:28) @ org.sonatype.mavenbook.custom.weather.yahoo.yahooparsertest.testparse r(yahooparsertest.java:36)

"unbound namespace prefix yweather"...??? <gulp!>

the following code works me:

import java.io.file;  import org.dom4j.document; import org.dom4j.documentexception; import org.dom4j.io.saxreader;  public class testsax {      public static void main(string[] args) throws documentexception {         saxreader xmlreader = new saxreader();         document doc = xmlreader.read(new file("d:/temp/sax.xml"));         string city_str = doc.valueof("/rss/channel/yweather:location/@city");         system.out.println( "=== doc value of city: |" + city_str + "|" );     } } 

i took dom4j , required jar jaxen here. check if use same libraries.

also note changed in code y:location yweather:location.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -