这是我第一次在网站上发布问题。我已经搜索了几天,可能有一个解决方案。但是,我一直没有找到任何可以解决我的问题的东西,所以我希望你们能帮助我解决问题。此外,我不太擅长java,所以这个问题可能会变得荒谬。
我正在尝试从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();
}
}
}
我的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