2
我使用xmlpullparser在android系統解析的XML文檔的樣子:解析XML XmlPullParser安卓
<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();
}
,但我不能找回我的歌曲列表。
任何人都可以幫我嗎?
謝謝您的回答,我已經按照在IBM developerWorks上[鏈接](例子http://www.ibm.com/developerworks/opensource/library/x -android /) – wael
@wael:不,你沒有。嘗試找到一個例子,在那裏他們檢查'name'是否等於一個值,然後*在該條件下*檢查'name'是否等於另一個值。他們檢查'currentMessage'爲空,並檢查其中的各種名稱,但這不是一回事。你現在明白爲什麼它不起作用嗎? –
請您解釋更多關於**您需要不斷地拉動XML並對循環中的每個元素名稱作出不同的反應** – wael