从API提要中解析XML(Java)

时间:2021-04-09 15:23:23

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.

这是我第一次在网站上发帖提问。我一直在寻找一个解决方案,并且可能有一个。但是,我找不到任何可以解决我问题的东西,所以我希望你能帮到我。另外,我不是最好的java,所以这个问题最终可能会变得非常愚蠢。

I am trying to parse a weather xml file from a url and I continue to receive a XPathExpression error.

我试图从URL解析天气xml文件,我继续收到XPathExpression错误。

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.)

我的LiveWeather类具有与下面的所有各种属性类似的语句。 (其中有很多人。)

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

I tried to include any information that might possibly be necessary. Thanks for the help in advanced, and sorry if this question was already asked.

我试图包含可能需要的任何信息。感谢高级的帮助,如果已经提出这个问题,对不起。

2 个解决方案

#1


2  

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 setNamespaceContextmethod to establish the namespace context before compiling the expression.

您的XPath表达式(// aws:weather)包含未绑定到任何名称空间URI的名称空间前缀(aws)。正如javax.xml.xpath.XPath的Javadoc中所解释的那样,“表达式中的QNames是针对使用setNamespaceContext(NamespaceContext nsContext)设置的XPath命名空间上下文解析的”。因此,在编译表达式之前,需要使用setNamespaceContextmethod来建立名称空间上下文。

#2


0  

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.

如果您正在尝试解析XML文档的架构,则可能需要考虑从架构生成JAXB绑定,这样您就可以将整个XML文档转换为Java对象图。

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/

既然你提到你的Java不是最好的结账这个免费的在线书“使用Java处理XML”http://www.ibiblio.org/xml/books/xmljava/

#1


2  

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 setNamespaceContextmethod to establish the namespace context before compiling the expression.

您的XPath表达式(// aws:weather)包含未绑定到任何名称空间URI的名称空间前缀(aws)。正如javax.xml.xpath.XPath的Javadoc中所解释的那样,“表达式中的QNames是针对使用setNamespaceContext(NamespaceContext nsContext)设置的XPath命名空间上下文解析的”。因此,在编译表达式之前,需要使用setNamespaceContextmethod来建立名称空间上下文。

#2


0  

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.

如果您正在尝试解析XML文档的架构,则可能需要考虑从架构生成JAXB绑定,这样您就可以将整个XML文档转换为Java对象图。

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/

既然你提到你的Java不是最好的结账这个免费的在线书“使用Java处理XML”http://www.ibiblio.org/xml/books/xmljava/