How to parse complex XML using XmlPullParser

2019-08-28 04:12发布

Hi guys I have problem with xml parsing. I'm trying to parse using XmlPullParser

XML looks like:

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

This is what i'm trying to do:

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

I looked Parsing XML Data but it didn't helps me.

Can you help me to parse: rank, name, url, image (size large)?

2条回答
Ridiculous、
2楼-- · 2019-08-28 04:32

I would recommend SimpleXML to you to parse more complex XML data or just use XPath

查看更多
冷血范
3楼-- · 2019-08-28 04:43

I ad this code and all work

<!-- 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);
        }
    }
查看更多
登录 后发表回答