使用XMLPullParser解析使用Android的iTunes RSS Atom提要

时间:2013-07-24 04:35:38

标签: java android xml-parsing itunes xmlpullparser

链接到iTunes chart

歌曲标题,专辑图片链接和歌曲的网页链接应以列表视图显示。

static final String KEY_ENTRY = "entry";
static final String KEY_TITLE = "title";
static final String KEY_TITLE_URL = "id";
static final String KEY_SONG_DOWNLOAD_URL = "link";
static final String KEY_ALBUM_IMAGE_URL = "im:image";

public static List<iTunes> getStackSitesFromFile(Context ctx) {

    // List of StackSites that we will return
    List<iTunes> itunes;
    itunes = new ArrayList<iTunes>();

    // temp holder for current StackSite while parsing
    iTunes curiTunes = null;
    // temp holder for current text value while parsing
    String curText = "";

    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();

        FileInputStream fis = ctx.openFileInput("iTunes.xml");
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                fis));

        xpp.setInput(reader);

        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagname = xpp.getName();

            switch (eventType) {
            case XmlPullParser.START_TAG:
                if (tagname.equalsIgnoreCase(KEY_ENTRY)) {
                    curiTunes = new iTunes();
                }
                break;

            case XmlPullParser.TEXT:
                curText = xpp.getText();
                break;

            case XmlPullParser.END_TAG:
                if (tagname.equalsIgnoreCase(KEY_ENTRY)) {

                    itunes.add(curiTunes);
                } else if (tagname.equalsIgnoreCase(KEY_TITLE)) {
                    curiTunes.setTitle(curText);
                } else if (tagname.equalsIgnoreCase(KEY_TITLE_URL)) {
                    curiTunes.setSongUrl(curText);
                } else if (tagname.equalsIgnoreCase(KEY_SONG_DOWNLOAD_URL)) {
                    curiTunes.setSongDownloadUrl(curText);
                } else if (tagname.equalsIgnoreCase(KEY_ALBUM_IMAGE_URL)) {
                    curiTunes.setAlbumImgUrl(curText);
                }
                break;

            default:
                break;
            }
            eventType = xpp.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return itunes;
}

}

0 个答案:

没有答案