RSS Feed Parsing, extracting field value

750 Views Asked by At

I am totally new in RSS feed parsing. What i am trying, is to get duration of mp3 file. But I am unable to get value from a specific tag

<itunes:duration>01:00:00</itunes:duration>

Here is My parsing code:

public List<RSSItem> parse() {
    final RSSItem currentMessage = new RSSItem();
    RootElement root = new RootElement("rss");
    final List<RSSItem> messages = new ArrayList<RSSItem>();

    Element channel = root.getChild("channel");
    Element item = channel.getChild(ITEM);
    item.setEndElementListener(new EndElementListener() {
        public void end() {
            messages.add(currentMessage.copy());
        }
    });
    item.getChild(TITLE).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setTitle(body);
                }
            });
    item.getChild(LINK).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setLink(body);
                }
            });
    item.getChild(DESCRIPTION).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setDescription(body);
                }
            });
    item.getChild("http://purl.org/rss/1.0/modules/content/", "encoded")
            .setEndTextElementListener(new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setContent(body);
                }
            });
    item.getChild(CONTENT).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setContent(body);
                }
            });
    item.getChild(PUB_DATE).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setDate(body);
                }
            });
    item.getChild(CATEGORY).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setCategory(body);
                }
            });
    Element enclosure = item.getChild(ENCLOSURE);
    enclosure.setStartElementListener(new StartElementListener() {

        public void start(Attributes attributes) {
            currentMessage.setAdioUrl(attributes.getValue("url"));
            // currentMessage.setAdioFileDuration(attributes.getValue("length"));
        }

    });
    item.getChild(ADIOFILEDuration).setEndTextElementListener(
            new EndTextElementListener() {
                public void end(String body) {
                    currentMessage.setAdioFileDuration(body);
                }
            }); //ADIOFILEDuration=itunes

    try {
        Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
                root.getContentHandler());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return messages;
}

And here is RSS Object:

 <item>
 <title>FantasyGuru.com Show - Jul 30,2009</title>
 <link>http://www.blogtalkradio.com/fantasyguru/2009/07/30/fantasygurucom-show</link>
.....
....
....
....
<itunes:duration>01:00:00</itunes:duration>
 <media:group>
  <media:content url="http://www.blogtalkradio.com/fantasyguru/2009/07/30/fantasygurucom-show.mp3" fileSize="14426407" type="audio/mpeg" /><media:content url="http://www.blogtalkradio.com/fantasyguru/2009/07/30/fantasygurucom-show.wma" fileSize="14426407" type="audio/x-ms-wma" />
 </media:group>
 <itunes:author>FantasyGuru</itunes:author>
 <itunes:explicit>no</itunes:explicit>
 <itunes:keywords>fantasy,NFL,sports,FantasyGuru,football,BlogTalkRadio, Blog Talk Radio</itunes:keywords>
 <itunes:subtitle>FantasyGuru.com Show</itunes:subtitle>
</item>
2

There are 2 best solutions below

0
On

http://docs.oracle.com/javase/tutorial/jaxb/intro/index.html

Have you had a look at JAXB? It's a nicer way to parse through XML and it might help solve your problem.

0
On

Alternatively, you may use this library to automatically handle your RSS feeds. https://github.com/Pkmmte/PkRSS

It supports RSS2 and Atom by default but you can extend your own Parser class based off the examples and plug it in using the following code. new PkRSS.Builder(this).parser(new CustomParser()).buildSingleton();