There is nothing wrong with the second loop, you have to navigate through the elements hierarchy to come to the once you are interested in, so you were already on right path. And here is how you could continue:
public class Dom4JRssParser {
private void parse(Date day) throws DocumentException, ParseException {
Date dayOnly = removeTime(day);
// Fri, 30 Aug 2013 07:00:00 GMT
SimpleDateFormat sdfXml = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
System.out.println("Day: " + sdfXml.format(dayOnly));
SAXReader reader = new SAXReader();
Document doc = reader.read(getClass().getResourceAsStream("/com/so/dom4j/parser/rss/example_01.xml"));
Element root = doc.getRootElement(); // rss
for(Iterator rootIt = root.elementIterator("channel"); rootIt.hasNext(); ) {
Element channel = (Element) rootIt.next();
for(Iterator itemIt = channel.elementIterator("item"); itemIt.hasNext(); ) {
Element item = (Element) itemIt.next();
Element pubDate = item.element("pubDate");
if(pubDate != null) {
if(removeTime(sdfXml.parse(pubDate.getTextTrim())).equals(dayOnly)) {
Rns rns = new Rns(item.element("title"),
item.element("link"),
item.element("description"),
item.element("constituent"));
System.out.println(rns.toString());
System.out.println();
}
}
}
}
}
private Date removeTime(Date day) {
Calendar c = Calendar.getInstance(Locale.ENGLISH);
c.setTime(day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static void main(String... args) throws ParseException, DocumentException {
Dom4JRssParser o = new Dom4JRssParser();
if(args.length == 0) {
o.parse(new Date());
} else {
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMdd");
for(String arg : args) {
o.parse(sdfInput.parse(arg));
}
}
}
}
Test run with the argument
20130731
Output
Day: Wed, 31 Jul 2013 00:00:00 CEST
Rns [rnsHeadline=Ariana Resources PLC - Directors' Dealings and Holding in Company
rnsLink=http://www.iii.co.uk/investment/detail?code=cotn:AAU.L&display=news&action=article&articleid=9053338&from=rss
rnsFullText=Some Article information
rnsConstituentName=]
Also you may consider using the XPath API (section Powerful Navigation with XPath
in the quick-start link you have posted) as it is more comfortable, see eis's answer.