这是我第一次在网站上发帖提问.我一直在寻找一个解决方案,并且可能有一个.但是,我找不到任何可以解决我问题的东西,所以我希望你能帮到我.另外,我不是最好的 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
我试图包含可能需要的任何信息.感谢高级的帮助,如果已经提出这个问题,对不起.
您的XPath表达式(// aws:weather)包含未绑定到任何名称空间URI的名称空间前缀(aws).正如javax.xml.xpath.XPath的Javadoc中所解释的那样,“表达式中的QNames是针对使用setNamespaceContext(NamespaceContext nsContext)设置的XPath名称空间上下文解析的”.因此,在编译表达式之前,需要使用setNamespaceContextmethod来建立名称空间上下文.