This is the first time I am posting a question on a site. I searched for a solution for this for a couple of days, and there could be one. However, I could not find anything to solve my questions, so I hope you can help me a lot. Also, I'm not the best in Java, so this question might turn out to be ridiculously stupid.
I am trying to parse Weather XML file by URL and keep getting XPathExpression error.
public class WeatherBugAPI { private XPath xpath = XPathFactory.newInstance().newXPath(); String API_KEY = "A***************"; public void getLiveWeather(String zipcode) throws Exception{ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); URL xml = new URL("http://A**********.api.wxbug.net/getLiveCompactWeatherRSS.aspx?acode=A**********&zipcode=" + zipcode); try{ InputStream is = xml.openStream(); Document document = builder.parse(is); XPathExpression expr = xpath.compile("//aws:weather"); Node node = (Node)xpath.evaluate("//aws:weather", is, XPathConstants.NODE); LiveWeather weather = new LiveWeather(xpath, node); System.out.println(weather); } catch(XPathExpressionException e){ System.out.println("Failed to parse forcast!"); e.printStackTrace(); } } }
my LiveWeather class has characteristics similar to the following for all the various attributes. (Theres a ton of them.)
stationZipcode = (String)xpath.evaluate("//aws:city-state/@zipcode", node, XPathConstants.STRING);
and finally, the stack trace:
Failed to parse forcast! javax.xml.transform.TransformerException: Unable to evaluate expression using this context at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275) at WeatherBugAPI.getLiveWeather(WeatherBugAPI.java:36) at test.main(test.java:5) Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212) at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335) ... 4 more --------- java.lang.RuntimeException: Unable to evaluate expression using this context at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212) at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275) at WeatherBugAPI.getLiveWeather(WeatherBugAPI.java:36) at test.main(test.java:5) --------------- linked to ------------------ javax.xml.xpath.XPathExpressionException at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:289) at WeatherBugAPI.getLiveWeather(WeatherBugAPI.java:36) at test.main(test.java:5) Caused by: javax.xml.transform.TransformerException: Unable to evaluate expression using this context at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275) ... 2 more Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212) at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335) ... 4 more
source share