java - Something wrong in printing XML node attribute value -


i have problem in printing value of attribute existing in node in given xml file.i use code , compiled correctly didn't print anything:

xpathfactory factory = xpathfactory.newinstance(); xpath xpath = factory.newxpath(); xpathexpression expr = xpath.compile("//rss/channel/yweather:location/@city"); object result = expr.evaluate(doc, xpathconstants.string); system.out.println(result); 

and xml file :

<?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 - sunnyvale, ca</title>   <link>http://us.rd.yahoo.com/dailynews/rss/weather/sunnyvale__ca/*http://weather.yahoo.com/forecast/usca1116_f.html</link>   <description>yahoo! weather sunnyvale, ca</description>   <language>en-us</language>   <lastbuilddate>fri, 18 dec 2009 9:38 pst</lastbuilddate>   <ttl>60</ttl>   <yweather:location city="sunnyvale" region="ca"   country="united states"/>   <yweather:units temperature="f" distance="mi" pressure="in" speed="mph"/> </channel> </rss> 

the following code works namespace reference in xpath. key points implementing namespacecontext , calling domfactory.setnamespaceaware(true)...

import java.util.iterator; import javax.xml.xmlconstants; import javax.xml.namespace.namespacecontext; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathexpression; import javax.xml.xpath.xpathfactory; import org.w3c.dom.document;  public class demo  {     public static void main(string[] args)      {         documentbuilderfactory domfactory = documentbuilderfactory.newinstance();         domfactory.setnamespaceaware(true);         try          {             documentbuilder builder = domfactory.newdocumentbuilder();             document ddoc = builder.parse("c:\\path\\to\\xml\\file.xml");             xpath xpath = xpathfactory.newinstance().newxpath();             xpath.setnamespacecontext(new universalnamespaceresolver(ddoc));             string query = "//rss/channel/yweather:location/@city";             xpathexpression expr = xpath.compile(query);             object result = expr.evaluate(ddoc, xpathconstants.string);             system.out.println(result);         }          catch (exception e)          {             e.printstacktrace();         }     }      public static class universalnamespaceresolver implements namespacecontext      {         private document sourcedocument;          public universalnamespaceresolver(document document)          {             sourcedocument = document;         }          public string getnamespaceuri(string prefix)          {             if (prefix.equals(xmlconstants.default_ns_prefix))                 return sourcedocument.lookupnamespaceuri(null);             else                 return sourcedocument.lookupnamespaceuri(prefix);         }          public string getprefix(string namespaceuri)          {             return sourcedocument.lookupprefix(namespaceuri);         }          public iterator getprefixes(string namespaceuri)          {             return null;         }     }    } 

be sure change file path before running.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -