这是我第一次在网站上发布问题。我一直在寻找一个解决方案,并且可能有一个。但是,我找不到任何可以解决我问题的东西,所以我希望你能帮到我。另外,我不是最好的java,所以这个问题最终可能会变得非常愚蠢。
我正在尝试从网址解析天气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();
}
}
}
我的LiveWeather类的所有各种属性都有类似于下面的类型的语句。 (其中有很多人。)
stationZipcode = (String)xpath.evaluate("//aws:city-state/@zipcode", node, XPathConstants.STRING);
最后是堆栈跟踪:
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
我试图包含可能需要的任何信息。感谢高级的帮助,如果已经提出这个问题,请对不起。
答案 0 :(得分:2)
您的XPath表达式(//aws:weather
)包含未绑定到任何名称空间URI的名称空间前缀(aws
)。正如javax.xml.xpath.XPath
的Javadoc中所解释的那样,表达式中的QNames是针对使用setNamespaceContext(NamespaceContext nsContext)设置的XPath命名空间上下文解析的。因此,在编译表达式之前,需要使用setNamespaceContext
方法来建立名称空间上下文。
答案 1 :(得分:0)
如果您正在尝试解析XML文档的架构,则可能需要考虑从架构生成JAXB绑定,这样您就可以将整个XML文档转换为Java对象图。
既然你提到你的Java不是最好的结账这个免费的在线书“用Java处理XML”http://www.ibiblio.org/xml/books/xmljava/