English 中文(简体)
Parsing XML XmlPull Parser android
原标题:Parsing XML XmlPullParser android

i m 采用xmlpullparser in android to parse an xml document that look as:

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

but I can not retrieve my Songs List.

can any one help me please ?

最佳回答

您似乎期待<代码> 姓名在检查之间作出重大改变:

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

如何使用<条码>。 。 你们需要保留XML,并对每个元素名称作出不同的反应。 因此,如果 声明在对《第一阶段裁武条约》活动作出反应时没有插手的话,你很可能有<条码>。 (将处理方式推向一种单独的方法可能非常值得。)

EDIT:Okae,因此,你必须使 lo的每 it都只对one tag作出反应。 因此,你对开端的处理就好像:

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

说明在任何途径中,我们从不核对<代码>名称/代码>。 具有多重价值观——我们说:

  • Are we starting a new category?
    • If so, create it and remember it - then continue with the next iteration.
    • If not (and if we ve got a category), are we starting a new song list?
    • If so, create it and remember it - then continue with the next iteration.
    • If not (and if we ve got a song list), are we starting a new song?
      • If so, create it and remember it - then continue with the next iteration.
      • If not (and if we ve got a song)...
      • Are we reading the title? If so, read the text and set it, then continue.
      • Are we reading the singer? If so, read the text and set it, then continue.
      • Are we reading the thumbnail? If so, read the text and set it, then continue.
      • etc
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...