2

I need to parse this XML using XmlPuppParser

<lfm status="ok">
    <events xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" artist="Armin van Buuren" festivalsonly="0" page="1" perPage="50" totalPages="1" total="17">
        <event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
            <id>3353053</id>
            <title>ULTRA Buenos Aires 2013 - Dia 2</title>
            <artists>
                <artist>Armin van Buuren</artist>
                ...
                <artist>Adrian de Bernardi</artist>
                <artist>Manu Desrets</artist>
                <headliner>Armin van Buuren</headliner>
            </artists>
            <venue>
                <id>8778836</id>
                <name>Costanera Sur</name>
                <location>
                    <city>Ciudad de Buenos Aires</city>
                    <country>Argentina</country>
                    <street>Av. Espana 2230</street>
                    <postalcode/>
                    <geo:point>
                        <geo:lat>-34.61135</geo:lat>
                        <geo:long>-58.35838</geo:long>
                    </geo:point>
                </location>
                <url>http://www.last.fm/venue/8778836+Costanera+Sur</url>
                <website/>
                <phonenumber/>
                <image size="small">http://userserve-ak.last.fm/serve/34/54798997.jpg</image>
                ....
                <image size="extralarge">http://userserve-ak.last.fm/serve/252/54798997.jpg</image>
            </venue>
            <startDate>Sat, 23 Feb 2013 17:58:01</startDate>
            <description/>
            <image size="small">http://userserve-ak.last.fm/serve/34/83063099.jpg</image>
            ....
            <image size="extralarge">http://userserve-ak.last.fm/serve/252/83063099.jpg</image>
            <attendance>45</attendance>
            <reviews>0</reviews>
            <tag>lastfm:event=3353053</tag>
            <url>http://www.last.fm/festival/3353053+ULTRA+Buenos+Aires+2013+-+D%C3%ADa+2</url>
            <website>http://ultrabuenosaires.com/</website>
            <tickets></tickets>
            <cancelled>0</cancelled>
            <tags>
                <tag>electronic</tag>
            </tags>
        </event>
        <event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
            ....
        </event>
    </events>
</lfm>

I need to parse this information: title, artists, city, country, geo, date and description

while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String tag = parser.getName();
            MyLog.d(TAG, "tag == " + tag);
            if (tag.equals("title")) {
                title = readTitle(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + title);
            } else if (tag.equals("artists")) {
                MyLog.d(TAG, "tag == " + tag);
            } else if (tag.equals("artist")) {
                if (artists == null) {
                    artists = readArtist(parser);
                } else {
                    artists += ", " + readArtist(parser);
                }
                MyLog.d(TAG, "tag == " + tag + ": " + artists);
            } else if (tag.equals("headliner")) {
                artists += ", " + readHeadliner(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + artists);
                parser.nextTag();
            } else if (tag.equals("venue")) {
                MyLog.d(TAG, "tag == " + tag);
            } else if (tag.equals("location")) {
                MyLog.d(TAG, "tag == " + tag);
            } else if (tag.equals("city")) {
                city = readCity(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + city);
            } else if (tag.equals("country")) {
                country = readCountry(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + country);
            } else if (tag.equals("geo:point")) {
                MyLog.d(TAG, "tag == " + tag);
            } else if (tag.equals("geo:lat")) {
                geoLat = readGeoLat(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + geoLat);
            } else if (tag.equals("geo:long")) {
                geoLong = readGeoLong(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + geoLong);
            } else if (tag.equals("startDate")) {
                date = readDate(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + date);
            } else if (tag.equals("description")) {
                description = readDescription(parser);
                MyLog.d(TAG, "tag == " + tag + ": " + description);
            } else {
                skip(parser);
            }

        }

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

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 parse all information, except date and description. I think my mistake is that after parsing the location does not find the next tag. Can you change my code or give me tutorial how to parse complex XML using XmlPullParser?

4

1 に答える 1

0

私はこのコードを使用しましたが、うまく機能しました。たとえば、このコードを使用してください。

START_TAGの解析中に、タグを解析するときに1つのカウンター変数を取得して1を割り当て、タグを取得するたびに1ずつインクリメントします。次に、タグの解析中に、タグが初めて来るかどうかを確認します。次に、目的の値を解析します。

 <spaces>
 <space>
 <name></name>
 <desc></desc>
<songs>
  <song>
    <name></name>
    <thumb></thumb>
  </song>
  <song>
    <name></name>
    <thumb></thumb>
  </song>
</songs>
</space>
</spaces>



 case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("space")) {
                    cnt=1;
                    currentSpace = new Space();
                    System.out.println("space");

                } else if (currentSpace != null) {

                    if (name.equalsIgnoreCase("name")) 
                     {
                        currentSpace.setName(parser.nextText());
                        if(cnt==1)
                        {
                            // you have <name> from <space>
                        }
                        System.out.println(":::"+currentSpace.getName());
                        cnt++;
                    } else if (name.equalsIgnoreCase("id")) {
                        if (currentSpace.getId() == null) {
                            currentSpace.setId(parser.nextText());
                        }

                    }

                }

                break;
于 2013-02-25T07:05:22.443 に答える