Come analizzare il complesso di XML utilizzando XmlPullParser

Ciao ragazzi, ho un problema con il parsing di xml. Sto cercando di analizzare utilizzando XmlPullParser

XML, assomiglia a questo:

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

Questo è quello che sto cercando di fare:

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

Ho guardato L’analisi dei Dati XML ma non mi aiuta.

Mi potete aiutare ad analizzare: rango, nome, l’url dell’immagine (grandi dimensioni)?

InformationsquelleAutor WOLVERINE | 2013-02-15



2 Replies
  1. 3

    Ho questo annuncio di codice e tutto il lavoro

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

Lascia un commento