如何使用SAXParse用java解析原子提要

时间:2014-01-24 20:25:58

标签: java parsing rss itunes atom-feed

我使用SAXParser来解析为iTunes播客准备的RSS提要。它不希望使用这些标签。如果我删除所有标记的标签,它将起作用。 从标记

获取信息的主要任务
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:cc="http://web.resource.org/cc/"      xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<channel>
            <!-- With this tags I get error -->
    <atom:link href="" rel="self" type="application/rss+xml"/>
    <title></title>
    <pubDate></pubDate>
    <lastBuildDate></lastBuildDate>
    <generator></generator>
    <link></link>
    <language>en</language>
    <copyright><![CDATA[txt]]></copyright>
    <docs></docs>
    <managingEditor></managingEditor>
    <description><![CDATA[txt]]></description>
    <image>
        <url></url>
        <title></title>
        <link><![CDATA[]]></link>
    </image>
    <itunes:author></itunes:author>
    <itunes:keywords></itunes:keywords>
    <itunes:image href="" />
    <itunes:explicit>no</itunes:explicit>
    <itunes:summary><![CDATA[]]></itunes:summary>
    <itunes:subtitle><![CDATA[]]></itunes:subtitle>**
    <!-- *************************************************** -->    
        <item>
        <title>Title 1</title>
        <pubDate>Tue, 31 Dec 2013 14:24:00 +0000</pubDate>
        <guid isPermaLink="false"><![CDATA[7ef6ab037fe7f9e77a195b42fba84017]]></guid>
        <link><![CDATA[http://www.123.com]]></link>
        <itunes:image href="http://123.com/item/2613394" />
        <description><![CDATA[]]></description>
        <enclosure length="41999781" type="audio/mpeg" url="http://123.mp3" />
        <itunes:duration>43:45</itunes:duration>
        <itunes:explicit>clean</itunes:explicit>
        <itunes:keywords>learning,howto</itunes:keywords>
        <itunes:subtitle><![CDATA[]]></itunes:subtitle>
    </item>
</channel>
</rss>

那是DefaultHandler:

public class StudyHandler extends DefaultHandler {
private List<Track> track;
private Track currentTrack;
private StringBuilder builder;

boolean isTitle = false;

public List<Track> getMessages() {
    return this.track;
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    super.characters(ch, start, length);
    builder.append(ch, start, length);
    if (isTitle) {
        currentTrack.title = builder.toString();
    }
}

@Override
public void startDocument() throws SAXException {
    super.startDocument();

    track = new ArrayList<Track>();
    builder = new StringBuilder();
}

@Override
public void startElement(String uri, String localName, String name,
        Attributes attributes) throws SAXException {
    super.startElement(uri, localName, name, attributes);
    String value = localName.trim();
    if (value.equalsIgnoreCase("ITEM")) {
        this.currentTrack = new Track();
    } else if (value.equalsIgnoreCase("TITLE")) {
        isTitle = true;
    }

}

@Override
public void endElement(String uri, String localName, String name)
        throws SAXException {
    super.endElement(uri, localName, name);

    if (this.currentTrack != null) {
        if (localName.equalsIgnoreCase("TITLE")) {
            isTitle = false;
        } else if (localName.equalsIgnoreCase("ITEM")) {
            track.add(currentTrack);
        }
        builder.setLength(0);
    }
}
}

1 个答案:

答案 0 :(得分:0)

当您尝试跟踪每个开始/结束标记操作或处于调试模式时,您是否注意到解析器正在解析标记?

安东尼