package com.example.xml;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class PullUtils {
private static List<Books> list;
private static Books b;
public static List<Books> getPull(InputStream inputStream) {
list = new ArrayList<Books>();
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(inputStream, "utf-8");
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
if ("book".equals(parser.getName())) {
b = new Books();
}else if ("price".equals(parser.getName())) {
b.setPrice(Integer.parseInt(parser.nextText()));
} else if ("name".equals(parser.getName())) {
b.setName(parser.nextText());
} else if ("author".equals(parser.getName())) {
b.setAuthor(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("book".equals(parser.getName())) {
list.add(b);
b=null;
}
break;
}
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
移动开发----Xml的pull解析方式
最新推荐文章于 2021-05-26 14:27:19 发布