2010-11-25 100 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

什麼異常說您正在吞嚥?我認爲這個異常信息會啓發你。