XML的Pull解析
//通过xml解析串
private void XMLtoStr(String result) {
News news=null;
try {
//将读取到的结果码 添加到XML里面
XmlPullParser Parser = Xml.newPullParser();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(result.getBytes());
Parser.setInput(byteArrayInputStream, "utf-8");
//通过parse获得项目类型 相当于文件里面的属性吧
int eventType = Parser.getEventType();
//判断
while ((eventType != XmlPullParser.END_DOCUMENT)) {
String name = Parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (name.equals("item")) {
news = new News();
} else if (name.equals("firstImg")) {
String firstImg = Parser.nextText();
news.setFirstImg(firstImg);
} else if (name.equals("source")) {
String source = Parser.nextText();
news.setSource(source);
} else if (name.equals("title")) {
String title = Parser.nextText();
news.setTitle(title);
} else if (name.equals("url")) {
String url = Parser.nextText();
news.setUrl(url);
}
break;
case XmlPullParser.END_TAG:
if (name.equals("item")) {
list.add(news);
}
break;
default:
break;
}
eventType=Parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};