Pull解析
//1.创建解析工厂对象
XmlPullParserFactory xmlFactory = XmlPullParserFactory.newInstance();
//2.获得解析器对象
XmlPullParser xmlParser = xmlFactory.newPullParser();
//3.指定资源路径
FileReader fr = new FileReader("F:\\book.xml");
xmlParser.setInput(fr);
int event = xmlParser.getEventType();
while(event != END_DOCUMENT){
switch(event){
case START_DOCUMENT://文档开始
System.out.println("=====开始解析文档======");
break;
case START_TAG://标签开始
String tagname = xmlParser.getName();//获得标签名称
switch(tagname){
case "name":
String name = xmlParser.nextText();
System.out.println(name);
break;
case "jiage":
String jiage = xmlParser.nextText();
System.out.println(jiage);
break;
case "zz":
String zz = xmlParser.nextText();
System.out.println(zz);
break;
case "cb":
String cb = xmlParser.nextText();
System.out.println(cb);
break;
}
break;
case END_TAG://标签结束
tagname = xmlParser.getName();
System.out.println("======文档解析结束=======");
break;
}
event = xmlParser.next();//获取下一个事件
}