0

I am using JDOM parser to parse a XML i created using feedrinse.

XML is available at: http://www.feedrinse.com/services/rinse/?rinsedurl=396ac3b0142bb6360003c8dbac4f8d47

My code is to parse every title,link,pubDate element and store them in a new XML which i am sending to front end. Here is my code:

String temp = "http://www.feedrinse.com/services/rinse/?rinsedurl=396ac3b0142bb6360003c8dbac4f8d47";
String XMLstr="<FEED>";

SAXBuilder builder = new SAXBuilder();
URL url= new URL(temp);
Document doc = null;                
 try{
    doc=builder.build(url);             
    Element root = doc.getRootElement();
    //List children = root.getChildren();
    List list = root.getChildren("item");
    XMLstr+="<children>"+list.size()+"</children>";
    for (int i = 0; i < list.size(); i++) {
           Element node = (Element) list.get(i);
           String title=node.getChildText("title").toString();
           XMLstr+="<title>"+title+"</title>";
           String link=node.getChildText("link").toString();
           XMLstr+="<link>"+link+"</link>";
           String desc=node.getChildText("description").toString();
           XMLstr+="<desc>"+desc+"</desc>";
           String pubDate=node.getChildText("pubDate").toString();
           XMLstr+="<pubDate>"+pubDate+"</pubDate>";           
        }
    }
catch(Exception e)
    {
    out.println(e);
    }
    XMLstr+="</FEED>";

However, it is not parsing correctly. At first it always show children size as 0. Please suggest what mistake i am doing and how can i rectify the same. Thanks.

CodeMonkey
  • 2,265
  • 9
  • 48
  • 94

2 Answers2

1

The XML has the following structure

<rss>
  <channel>
    ...
    <item></item>
    <item></item>

E.g. Element root = doc.getRootElement() will return the rss element which does not have any item children.

EDIT: Try the following line

List list = root.getChild("channel").getChildren("item");
erikxiv
  • 3,965
  • 1
  • 23
  • 22
  • Can i use root.getChild().getChildren("item") then ? – CodeMonkey May 05 '12 at 14:08
  • this ant working as well `code`Element root = doc.getRootElement(); Element channel=root.getChild(); //List children = root.getChildren(); List list = channel.getChildren("item");`code` – CodeMonkey May 05 '12 at 14:46
  • Thank you sir.. even i tried something like that but may be it had some syntax issue. I tried reading the API doc and it said namespace for using getChild. So i was bit confused. Can you please share a good resource for this parsing. – CodeMonkey May 05 '12 at 17:34
  • Actually, I used the API docs (http://www.jdom.org/docs/apidocs/org/jdom2/Element.html) for reference as well. The getChild(ren) methods exist in several versions, both with and without namespace. – erikxiv May 05 '12 at 20:06
0

JDOM2 makes things easier tooo....

public static void main(String[] args) throws MalformedURLException, JDOMException, IOException {
    final URL url = new URL("http://www.feedrinse.com/services/rinse/?rinsedurl=396ac3b0142bb6360003c8dbac4f8d47");
    SAXBuilder sbuilder = new SAXBuilder();
    Document doc = sbuilder.build(url);
    XPathExpression<Element> itemxp = XPathFactory.instance().compile("/rss/channel/item", Filters.element());

    Element feed = new Element("FEED");

    List<Element> items = itemxp.evaluate(doc);
    Element kidcount = new Element("children");
    kidcount.setText("" + items.size());
    feed.addContent(kidcount);
    for (Element item : items) {

        addItem(feed, item, "title");
        addItem(feed, item, "link");
        addItem(feed, item, "description");
        addItem(feed, item, "pubDate");
    }

    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());

    //xout.output(doc, System.out);
    xout.output(feed, System.out);
}

private static void addItem(Element feed, Element item, String name) {
    Element emt = item.getChild(name);
    if (emt == null) {
        return;
    }
    feed.addContent(emt.clone());
}
rolfl
  • 17,539
  • 7
  • 42
  • 76