public List<News> parser(Context context, InputStream in) {
XmlPullParser newPullParser = Xml.newPullParser();
// XmlResourceParser newPullParser = context.getResources().getXml(in);
try {
newPullParser.setInput(in, "utf-8");
Log.e("", "值" + in);
int type = newPullParser.getEventType();
Log.e("", "lei型" + type);
while (type != XmlResourceParser.END_DOCUMENT) {
String name = newPullParser.getName();
switch (type) {
case XmlResourceParser.START_DOCUMENT:
list = new ArrayList<News>();
break;
case XmlResourceParser.START_TAG:
if ("news".equalsIgnoreCase(name)) {
news = new News();
} else if ("title".equalsIgnoreCase(name)) {
news.setTitle(newPullParser.nextText());
} else if ("body".equalsIgnoreCase(name)) {
news.setBody(newPullParser.nextText());
} else if ("author".equalsIgnoreCase(name)) {
news.setAuthor(newPullParser.nextText());
} else if ("pubDate".equalsIgnoreCase(name)) {
news.setPubDate(newPullParser.nextText());
} else if ("url".equalsIgnoreCase(name)) {
news.setUrl(newPullParser.nextText());
} else if ("newstype".equalsIgnoreCase(name)) {
newstype = new Newstype();
} else if ("type".equalsIgnoreCase(name)) {
news.setType(Integer.parseInt(newPullParser
.nextText()));
} else if ("authoruid2".equalsIgnoreCase(name)) {
news.setAuthoruid2(Integer.parseInt(newPullParser
.nextText()));
} else if ("eventurl".equalsIgnoreCase(name)) {
news.setEventurl(newPullParser.nextText());
}
break;
case XmlResourceParser.END_TAG:
if ("news".equalsIgnoreCase(name)) {
list.add(news);
news = null;
}
break;
}
type = newPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
Xml解析pull
最新推荐文章于 2024-03-19 20:23:14 发布