1

how can I parse XML like this

<rss version="0.92">
<channel>
<title>MyTitle</title>
<link>http://myurl.com</link>
<description>MyDescription</description>
<lastBuildDate>SomeDate</lastBuildDate>
<docs>http://someurl.com</docs>
<language>SomeLanguage</language>

<item>
    <title>TitleOne</title>
    <description><![CDATA[Some text.]]></description>
    <link>http://linktoarticle.com</link>
</item>

<item>
    <title>TitleTwo</title>
    <description><![CDATA[Some other text.]]></description>
    <link>http://linktoanotherarticle.com</link>
</item>

</channel>
</rss>

please any one help.

1 Answer 1

1

try this

public class ExampleHandler extends DefaultHandler {

private Channel channel;
private Items items;
private Item item;
private boolean inItem = false;

private StringBuilder content;

public ExampleHandler() {
    items = new Items();
    content = new StringBuilder();
}

public void startElement(String uri, String localName, String qName, 
        Attributes atts) throws SAXException {
    content = new StringBuilder();
    if(localName.equalsIgnoreCase("channel")) {
        channel = new Channel();
    } else if(localName.equalsIgnoreCase("item")) {
        inItem = true;
        item = new Item();
    }
}

public void endElement(String uri, String localName, String qName) 
        throws SAXException {
    if(localName.equalsIgnoreCase("title")) {
        if(inItem) {
            item.setTitle(content.toString());
        } else {
            channel.setTitle(content.toString());
        }
    } else if(localName.equalsIgnoreCase("link")) {
        if(inItem) {
            item.setLink(content.toString());
        } else {
            channel.setLink(content.toString());
        }
    } else if(localName.equalsIgnoreCase("description")) {
        if(inItem) {
            item.setDescription(content.toString());
        } else {
            channel.setDescription(content.toString());
        }
    } else if(localName.equalsIgnoreCase("lastBuildDate")) {
        channel.setLastBuildDate(content.toString());
    } else if(localName.equalsIgnoreCase("docs")) {
        channel.setDocs(content.toString());
    } else if(localName.equalsIgnoreCase("language")) {
        channel.setLanguage(content.toString());
    } else if(localName.equalsIgnoreCase("item")) {
        inItem = false;
        items.add(item);
    } else if(localName.equalsIgnoreCase("channel")) {
        channel.setItems(items);
    }
}

public void characters(char[] ch, int start, int length) 
        throws SAXException {
    content.append(ch, start, length);
}

public void endDocument() throws SAXException {
    // you can do something here for example send
    // the Channel object somewhere or whatever.
}

}

where Item,Items and Channel are getter setter class ...

to know more visit this question How to parse XML using the SAX parser

Sign up to request clarification or add additional context in comments.

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.