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.

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

4 Comments

Can i use root.getChild().getChildren("item") then ?
this ant working as well codeElement root = doc.getRootElement(); Element channel=root.getChild(); //List children = root.getChildren(); List list = channel.getChildren("item");code
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.
Actually, I used the API docs (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.
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());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.