简体   繁体   中英

Parsing XML from API feed (Java)

This is my first time posting a question on the site. I have been searching for a solution for this for a couple days, and there might be one out there. However, I haven't been able to find anything to solve my questions, so I'm hoping you lot can help me out. Also, I'm not the best at java, so this question might end up being ridiculously stupid.

I am trying to parse a weather xml file from a url and I continue to receive a 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 statments similar to the one below 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

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

If you have a schema for the XML document you are trying to parse you might want to consider generating a JAXB binding from the schema, that way you can turn the whole XML document into a Java object graph.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM