XPath无法正常工作

时间:2010-11-25 23:49:04

标签: java xpath

我有以下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

1 个答案:

答案 0 :(得分:3)

异常说你吞咽了什么?我认为这个异常消息会启发你。