XML配置文件
解析XML文档
public static void main(String[] args) throws Exception {
//创建解析工厂对象
XmlPullParserFactory xmlFactory = XmlPullParserFactory.newInstance();
//获得解析器对象
XmlPullParser xmlParser = xmlFactory.newPullParser();
//指定资源路径
FileReader fr = new FileReader("E:\\abc.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 name1 = xmlParser.nextText();
System.out.println(name1);
break;
case "year":
String y = xmlParser.nextText();
System.out.println(y);
break;
case "class":
String cla = xmlParser.nextText();
System.out.println(cla);
break;
}
break;
case END_TAG: //标签结束
tagname = xmlParser.getName();
System.out.println("文档解析结束");
break;
}
event = xmlParser.next();//获取下一个事件
}
}