标签:
<?xml version="1.0" encoding="UTF-8"?> <students> <student id="1001"> <stuno>1001</stuno> <name>张三</name> <sex>男</sex> <grade>Java一班</grade> </student> <student id="1002"> <stuno>1002</stuno> <name>李四</name> <sex>女</sex> <grade>Java二班</grade> </student> <student id="1003"> <stuno>1003</stuno> <name>小二</name> <sex>男</sex> <grade>Java三班</grade> </student> <student id="1004"> <stuno>1004</stuno> <name>小蜡</name> <sex>女</sex> <grade>Java四班</grade> </student> </students>
1 package xmlpulldemo; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 6 import org.xmlpull.v1.XmlPullParser; 7 import org.xmlpull.v1.XmlPullParserException; 8 import org.xmlpull.v1.XmlPullParserFactory; 9 10 public class XmlPullDemo { 11 12 public static void main(String[] args) throws XmlPullParserException, IOException { 13 14 // 创建一个解析工厂 15 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 16 // 由工厂创建一个解析器对象 17 XmlPullParser parser = factory.newPullParser(); 18 // 打开xml文档对应的输入流,填写xml文档的路径 19 parser.setInput(new FileInputStream("src/xmlwendang/student.xml"), "UTF-8"); 20 // 获得事件类型 21 int eventType = parser.getEventType(); 22 System.out.println(eventType == XmlPullParser.START_DOCUMENT); 23 do { 24 switch (eventType) { 25 case XmlPullParser.START_DOCUMENT: 26 System.out.println("读取开始,开始文档!"); 27 break; 28 case XmlPullParser.START_TAG: 29 String tagName = parser.getName(); 30 if (tagName.equals("student")) { 31 System.out.println("读取进度-开始标签:" + parser.getName() + ":" + parser.getAttributeName(0) + ":" 32 + parser.getAttributeValue(0)); 33 } else if (tagName.equals("stuno")) { 34 System.out.println("读取进度-学号:" + parser.nextText()); 35 } else if (tagName.equals("name")) { 36 System.out.println("读取进度-姓名:" + parser.nextText()); 37 } else if (tagName.equals("sex")) { 38 System.out.println("读取进度-性别:" + parser.nextText()); 39 } else if (tagName.equals("grade")) { 40 System.out.println("读取进度-班级:" + parser.nextText()); 41 } else if (tagName.equals("students")) { 42 System.out.println("读取开始-开始根目录:" + parser.getName()); 43 } 44 break; 45 case XmlPullParser.END_TAG: 46 if (parser.getName().equals("student")) { 47 System.out.println("读取进度-结束标签:" + parser.getName()); 48 } else if (parser.getName().equals("students")) { 49 System.out.println("读取完成-结束根目录:" + parser.getName()); 50 } 51 break; 52 } 53 // 读取下一个节点 54 eventType = parser.next(); 55 } while (eventType != XmlPullParser.END_DOCUMENT); 56 System.out.println("读取完成,文档结束!"); 57 } 58 59 }
结果:
1 true 2 读取开始,开始文档! 3 读取开始-开始根目录:students 4 读取进度-开始标签:student:id:1001 5 读取进度-学号:1001 6 读取进度-姓名:张三 7 读取进度-性别:男 8 读取进度-班级:Java一班 9 读取进度-结束标签:student 10 读取进度-开始标签:student:id:1002 11 读取进度-学号:1002 12 读取进度-姓名:李四 13 读取进度-性别:女 14 读取进度-班级:Java二班 15 读取进度-结束标签:student 16 读取进度-开始标签:student:id:1003 17 读取进度-学号:1003 18 读取进度-姓名:小二 19 读取进度-性别:男 20 读取进度-班级:Java三班 21 读取进度-结束标签:student 22 读取进度-开始标签:student:id:1004 23 读取进度-学号:1004 24 读取进度-姓名:小蜡 25 读取进度-性别:女 26 读取进度-班级:Java四班 27 读取进度-结束标签:student 28 读取完成-结束根目录:students 29 读取完成,文档结束!
标签:
原文地址:http://www.cnblogs.com/liuqingzi/p/5740677.html