2010-11-25 78 views
2

我有以下Java代码与XPath一起使用以从XML元素获取数据。第一次在title中表达式被消除时,它的工作原理应该如此。但下一次,在cost中,只要我显示cost的值,它就是“无法获得成本”。尝试执行多个XPath表达式时是否缺少某些内容?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

+0

那么,XML是怎么样的? – 2010-11-25 23:54:48

+0

尝试过吗?:// rss/channel/item/title和// rss/channel/item/pubDate(double //在开始处) – Steven 2010-11-25 23:54:55

回答

3

什么异常说您正在吞咽?我认为这个异常信息会启发你。