简体   繁体   中英

How to use SAXParse to parse atom feed with java

I use the SAXParser to parse RSS feed that was prepared for iTunes podcast. It doesn't want work with these tags. If I remove all marked tags it will work. The mainly task to get info from item tags

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

That's 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);
    }
}
}

When you try to trace each start/end tag operation or in debug mode, do you note that the parser is parsing the tags?

Anthony

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM