I have the following Java code working with XPath to get data from XML elements. The first time the expression is evaulated in title, it works as it should. But the next time, in cost, whenever I display the value of cost, it is just “Could not get cost”. Am I missing something when trying to do multiple XPath expressions?
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
}
This is the XML I’m going off of: http://www.woot.com/salerss.aspx
What does the exception say which you are swallowing? I think that this exception message will enlighten you.