2

私はAndroidでxmlpullparserを使用して、次のようなxmlドキュメントを解析しています:

<top>
  <category>
    <name></name>
    <desc></desc>
    <songs>
      <song>
        <clip></clip>
        <thumb></thumb>
      </song>
      <song>
        <clip></clip>
        <thumb></thumb>
      </song>
    </songs>
  </category>
</top>

私はこれを試しました:

while (eventType != XmlPullParser.END_DOCUMENT && !done){
                String name = null;
                switch (eventType){
                    case XmlPullParser.START_DOCUMENT:
                        categoriesSong = new ArrayList<TopMousika>();
                        break;
                    case XmlPullParser.START_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase(CATEGORY)){
                            currentCategory = new TopMousika();
                            currentCategory.setId(parser.getAttributeValue(0));
                            currentCategory.setId(parser.getAttributeValue(1));
                        } else if (currentCategory != null){
                            if (name.equalsIgnoreCase(NAME)){
                                currentCategory.setName(parser.nextText());
                            } else if (name.equalsIgnoreCase(DESCRIPTION)){
                                currentCategory.setDescription(parser.nextText());
                            } else if (name.equalsIgnoreCase(THUMBNAIL)){
                                currentCategory.setThumbnail(parser.nextText());
                            } else if (name.equalsIgnoreCase(SONGS)){
                                songs = new ArrayList<SongMousika>();
                                if(name.equalsIgnoreCase(SONG)){
                                    currentSong = new SongMousika();
                                    currentSong.setId(parser.getAttributeValue(0));
                                    Log.d("TEST", "OK");
                                    songs.add(currentSong);
                                } else if (name.equalsIgnoreCase(TITLE)){
                                    Log.d("TEST", "OK2");
                                    currentSong.setTitle(parser.nextText());
                                } else if (name.equalsIgnoreCase(SINGER)){
                                    currentSong.setTitle(parser.nextText());
                                } else if (name.equalsIgnoreCase(THUMBNAIL)){
                                    currentSong.setTitle(parser.nextText());
                                } else if (name.equalsIgnoreCase(PUBLICATION_DATE)){
                                    currentSong.setTitle(parser.nextText());
                                } else if (name.equalsIgnoreCase(CLIP)){
                                    currentSong.setTitle(parser.nextText());
                                }
                                currentCategory.setSongs(songs);
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase(CATEGORY) && 
currentCategory != null){
                            currentCategory.setSongs(songs);
                            categoriesSong.add(currentCategory);
                        } else if (name.equalsIgnoreCase(TOP)){
                            done = true;
                        }
                        break;
                }
                eventType = parser.next();
            }

曲リストを取得できません。

誰か助けてくれませんか?

4

1 に答える 1

4

nameチェック間で魔法のように変化することを期待しているようです。

if (name.equalsIgnoreCase(SONGS)) {
    songs = new ArrayList<SongMousika>();
    if(name.equalsIgnoreCase(SONG)) {

どうなるSONG SONGSXMLをプルし続け、ループ内で各要素名に異なる方法で反応する必要があります。したがってif/else if、START_TAGイベントに反応するときに、ネストせずに一連のステートメントが作成される可能性があります。(ちなみに、その処理を別の方法にまとめる価値がある可能性が非常に高いです。)

編集:さて、ループの各反復を1つのタグに反応させる必要があります。したがって、開始タグの処理は次のようになります。

case XmlPullParser.START_TAG:
    name = parser.getName();
    if (name.equalsIgnoreCase(CATEGORY)){
        currentCategory = new TopMousika();
        currentCategory.setId(parser.getAttributeValue(0));
        currentCategory.setId(parser.getAttributeValue(1));
    } else if (currentCategory != null) {
        if (name.equalsIgnoreCase(NAME)){
            currentCategory.setName(parser.nextText());
        } else if (name.equalsIgnoreCase(DESCRIPTION)){
            currentCategory.setDescription(parser.nextText());
        } else if (name.equalsIgnoreCase(THUMBNAIL)){
            currentCategory.setThumbnail(parser.nextText());
        } else if (name.equalsIgnoreCase(SONGS)){
            songs = new ArrayList<SongMousika>();
        } else if (songs != null) {
            if(name.equalsIgnoreCase(SONG)) {
                currentSong = new SongMousika();
                currentSong.setId(parser.getAttributeValue(0));
                Log.d("TEST", "OK");
                songs.add(currentSong);
            } else if (currentSong != null) {
                 else if (name.equalsIgnoreCase(TITLE)) {
                    Log.d("TEST", "OK2");
                    currentSong.setTitle(parser.nextText());
                } else if (name.equalsIgnoreCase(SINGER)){
                    currentSong.setSinger(parser.nextText());
                } else if (name.equalsIgnoreCase(THUMBNAIL))
                    // etc
                }
            }
        }

そこを通るどのパスでも、name複数の値があるかどうかをチェックしないことに注意してください。

  • 新しいカテゴリーを始めていますか?
    • もしそうなら、それを作成して覚えておいてください-そして次の反復を続けてください。
    • そうでない場合(そしてカテゴリある場合)、新しい曲のリストを開始しますか?
    • もしそうなら、それを作成して覚えておいてください-そして次の反復を続けてください。
    • そうでない場合(そして曲リストある場合)、新しい曲を始めますか?
      • もしそうなら、それを作成して覚えておいてください-そして次の反復を続けてください。
      • そうでない場合(そして私たち歌を持っている場合)...
      • タイトルを読んでいますか?その場合は、テキストを読んで設定してから続行してください。
      • 私たちは歌手を読んでいますか?その場合は、テキストを読んで設定してから続行してください。
      • サムネイルを読んでいますか?その場合は、テキストを読んで設定してから続行してください。
于 2011-09-03T07:58:14.273 に答える