java - RSS Feed 解析,提取字段值

标签 java android rss rss-reader

我对 RSS 提要解析完全陌生。我正在尝试的是获取 mp3 文件的持续时间。但是我无法从特定标签中获取值(value)

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

这是我的解析代码:

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;
}

这里是 RSS 对象:

 <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>

最佳答案

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

您看过 JAXB 了吗?这是一种通过 XML 进行解析的更好方法,它可能有助于解决您的问题。

关于java - RSS Feed 解析,提取字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483428/

相关文章:

java 将一个数组除以它的元素

Python:循环遍历文件中的url

有效 URL 的 java.io.FileNotFoundException

java - 在使用自定义 genericElement 的情况下如何修复 "No generic element handlers found for namepsace"错误?

java - java中比较两个字符串 -compareTo

java - Java 类中具有一对多关系的循环依赖关系

Android平台支持的硬件架构

android - Android 上的 Soap 与 Rest

Android - 跟踪应用程序在设备上的首次启动时间

parsing - 我正在尝试使用Powershell解析非标准的RSS提要