我有以下 Java 代码,使用 XPath 从 XML 元素获取数据。第一次在 title
中计算表达式时,它会按预期工作。但下一次,在 cost
中,每当我显示 cost
的值时,它只是“无法获取成本”。尝试执行多个 XPath 表达式时我是否遗漏了某些内容?
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression exp;
InputSource inputSource = new InputSource(new StringReader(woot_xml));
title = "Could not get title";
cost = "Could not get cost";
try {
exp = xpath.compile("/rss/channel/item/title");
title = exp.evaluate(inputSource);
exp = xpath.compile("/rss/channel/item/pubDate");
cost = exp.evaluate(inputSource);
} catch (XPathExpressionException e) {
// Do nothing for now
}
这是我要使用的 XML:http://www.woot.com/salerss.aspx
最佳答案
您正在吞咽的异常说明了什么?我认为这个异常消息会对您有所启发。
关于java - XPath 无法正确运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4281599/