XML返回实例
<song_list list="true">
<song>
<artist_id>88artist_id>
<language>国语language>
<pic_big>
http://musicdata.baidu.com/data2/pic/f201bdf2c3c41cef7b74985aee3b7e23/266941936/266941936.jpg
pic_big>
<pic_small>
http://musicdata.baidu.com/data2/pic/c4ed8fc38ecc1d69c87e5c5183fe3f65/266941939/266941939.jpg
pic_small>
<country>内地country>
<area>0area>
<publishtime>2016-06-23publishtime>
<album_no>1album_no>
<lrclink>
http://musicdata.baidu.com/data2/lrc/60902a2420610e8d3ac7ee2f9e754dc1/266942158/266942158.lrc
lrclink>
<copy_type>1copy_type>
<hot>750172hot>
<all_artist_ting_uid>2517all_artist_ting_uid>
<all_artist_id>88all_artist_id>
<style>影视原声style>
<all_rate>64,128,256,320,flacall_rate>
<versions>影视原声versions>
<song_id>266942077song_id>
<title>我好像在哪见过你title>
<ting_uid>2517ting_uid>
<author>薛之谦author>
<album_id>266941947album_id>
<album_title>我好像在哪见过你album_title>
<artist_name>薛之谦artist_name>
song>
XML PULL 解析
public static List<Music> parseMusicList(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int eventType = parser.getEventType();
List<Music> musics = new ArrayList<Music>();
Music music = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
String name = parser.getName();
if (name.equals("song")) {
music = new Music();
musics.add(music);
} else if (name.equals("pic_big")) {
music.setPic_big(parser.nextText());
} else if (name.equals("pic_small")) {
music.setPic_small(parser.nextText());
} else if (name.equals("publishtime")) {
music.setPublishtime(parser.nextText());
} else if (name.equals("lrclink")) {
music.setLrclink(parser.nextText());
} else if (name.equals("song_id")) {
music.setSong_id(parser.nextText());
} else if (name.equals("title")) {
music.setTitle(parser.nextText());
} else if (name.equals("author")) {
music.setAuthor(parser.nextText());
} else if (name.equals("album_id")) {
music.setAlbum_id(parser.nextText());
} else if (name.equals("album_title")) {
music.setAlbum_title(parser.nextText());
} else if (name.equals("artist_name")) {
music.setArtist_name(parser.nextText());
}
break;
}
eventType = parser.next();
}
return musics;
}
PULL解析是使用事件驱动的方式,边读取边解析。
当PULL解析器遇到文档中的某个节点时,都会触发
相应事件,我们可以捕获这些事件,并且在该时间
点解析相关内容。
如何使用PULL的方式解析xml?
1>创建xmlpull解析器:
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is);
2>获取事件类型,并且配合while循环,不断驱动事件
并且解析文档内容:
int type=parser.getEventType();
while(type != XmlPullParser.END_DOCUMENT){
根据事件类型解析文档
向后继续驱动事件
type = parser.next();
}