Parsing XML from an API feed (Java)

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 
+4
source share
2 answers

Your XPath expression ( //aws:weather ) contains a namespace prefix ( aws ) that is not bound to a namespace URI. As explained in javadoc javax.xml.xpath.XPath , "QNames in the expression resolve against the XPath namespace context specified with setNamespaceContext (NamespaceContext nsContext)." Therefore, you need to use the setNamespaceContext method to set the namespace context before compiling the expression.

+2
source

If you have an XML document schema that you are trying to execute, you might consider creating a JAXB binding from a schema, so you can turn an entire XML document into a Java graphic.

Since you mentioned that your Java is not the best test of this free online book, XML Processing with Java, http://www.ibiblio.org/xml/books/xmljava/

0
source

Source: https://habr.com/ru/post/1398347/


All Articles