如何使用XmlPullParser解析复杂的XML

时间:2013-02-15 07:48:52

标签: java android parsing xml-parsing xmlpullparser

大家好我有xml解析的问题。我正在尝试使用XmlPullParser解析

XML看起来像:

<lfm status="ok">
    <topartists user="dailz" type="overall" page="1" perPage="50" totalPages="30" total="1493">
        <artist rank="1">
            <name>Oasis</name>
            <url>http://www.last.fm/music/Oasis</url>
            <image size="small">http://userserve-ak.last.fm/serve/34/44937531.jpg</image>
            <image size="large">http://userserve-ak.last.fm/serve/126/44937531.jpg</image>
        </artist>
        <artist rank="2">
            ...
        </artist>
    </topartists>
</lfm>

这就是我想要做的事情:

public class TopArtistsParser {

    private static final String ns = null;
    private static final String TAG = "TopArtistsParser";

    public class Artist {

        public final String rank;
        public final String name;
        public final String url;
        public final String image;

        private Artist(String rank, String name, String url, String image) {
            this.rank = rank;
            this.name = name;
            this.url = url;
            this.image = image;
        }

    }

    public List<Artist> parse(InputStream in) throws XmlPullParserException,
            IOException {
        MyLog.d(TAG, "List<Artist> parse");
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }

    private List<Artist> readFeed(XmlPullParser parser)
            throws XmlPullParserException, IOException {

        MyLog.d(TAG, "List<Artist> readFeed");
        List<Artist> artistsList = new ArrayList<Artist>();

        parser.require(XmlPullParser.START_TAG, ns, "lfm");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String tag = parser.getName();
            if (tag.equals("artist")) {
                MyLog.d(TAG, "tag == 'artist'");
                artistsList.add(readArtist(parser));
            } else {
                skip(parser);
            }
        }

        return artistsList;
    }

    private Artist readArtist(XmlPullParser parser)
            throws XmlPullParserException, IOException {

        MyLog.d(TAG, "Artist readArtist");
        parser.require(XmlPullParser.START_TAG, ns, "artist");

        String rank = null;
        String name = null;
        String url = null;
        String image = null;

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String tag = parser.getName();
            if (tag.equals("name")) {
                name = readName(parser);
                MyLog.d(TAG, "tag == 'name': " + name);
            } else if (tag.equals("url")) {
                url = readUrl(parser);
                MyLog.d(TAG, "tag == 'url': " + url);
            } else if (tag.equals("image")) {
                image = readImage(parser);
                MyLog.d(TAG, "tag == 'image': " + image);
            } else {
                skip(parser);
            }
        }

        return new Artist(rank, name, url, image);
    }

    private String readName(XmlPullParser parser) throws IOException,
            XmlPullParserException {
        MyLog.d(TAG, "readName");
        parser.require(XmlPullParser.START_TAG, ns, "name");
        String name = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "name");
        return name;
    }

    private String readUrl(XmlPullParser parser) throws IOException,
            XmlPullParserException {
        MyLog.d(TAG, "readUrl");
        parser.require(XmlPullParser.START_TAG, ns, "url");
        String url = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "url");
        return url;
    }

    private String readImage(XmlPullParser parser) throws IOException,
            XmlPullParserException {
        MyLog.d(TAG, "readImage");
        parser.require(XmlPullParser.START_TAG, ns, "image");
        String image = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "image");
        return image;
    }

    private String readText(XmlPullParser parser) throws IOException,
            XmlPullParserException {
        MyLog.d(TAG, "readText");
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

    private void skip(XmlPullParser parser) throws XmlPullParserException,
            IOException {

        MyLog.d(TAG, "skip");

        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
    }
}

我看了Parsing XML Data,但这对我没有帮助。

你能帮我解析一下:排名,名字,网址,图片(大小)?

2 个答案:

答案 0 :(得分:3)

我宣传此代码和所有工作

<!-- language: java -->

while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String tag = parser.getName();
        if (tag.equals("topartists")) {
            MyLog.d(TAG, "tag == " + tag);
            tag = parser.getName();

        } else if (tag.equals("artist")) {
            MyLog.d(TAG, "tag == " + tag);
            artistsList.add(readArtist(parser));
        } else {
            skip(parser);
        }
    }

答案 1 :(得分:1)

我建议您SimpleXML解析更复杂的XML数据,或者只使用XPath