1、http:/ / 13Sax、pull 方式解析 Xmlsax 方式解析 xmlxml 文件 :张三男1987-06-08好学生李四女1987-06-08好学生小王男1987-06-08好学生小张男1987-06-08好学生小明男1987-06-08好学生Xml 对应的实体 beanpackage com.parsexml.entity;public class Student private int id;private int group;private String name;private String sex;private int age;private String email;
2、private String memo;private String birthday;public int getId() return id;public void setId(int id) this.id = id;public int getGroup() return group;public void setGroup(int group) this.group = group;public String getName() return name;public void setName(String name) this.name = name;public String ge
3、tSex() return sex;public void setSex(String sex) this.sex = sex;http:/ / 13public int getAge() return age;public void setAge(int age) this.age = age;public String getEmail() return email;public void setEmail(String email) this.email = email;public String getMemo() return memo;public void setMemo(Str
4、ing memo) this.memo = memo;public String getBirthday() return birthday;public void setBirthday(String birthday) this.birthday = birthday;sax 方式解析代码package com.parsexml;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.Pars
5、erConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import com.parsexml.entity.Student;/* 功能描述 :采用 sax 方式解析 XML* * author sxyx2008*/public class S
6、axParseXml extends DefaultHandler/存放遍历集合private List list;/构建 Student 对象private Student student;/用来存放每次遍历后的元素名称(节点名称)private String tagName;public List getList() return list;public void setList(List list) this.list = list;public Student getStudent() return student;public void setStudent(Student stud
7、ent) this.student = student;public String getTagName() return tagName;public void setTagName(String tagName) this.tagName = tagName;http:/ / 13/只调用一次 初始化 list 集合 Overridepublic void startDocument() throws SAXException list=new ArrayList();/调用多次 开始解析Overridepublic void startElement(String uri, String
8、 localName, String qName,Attributes attributes) throws SAXException if(qName.equals(“student“)student=new Student();/获取 student 节点上的 id 属性值student.setId(Integer.parseInt(attributes.getValue(0);/获取 student 节点上的 group 属性值student.setGroup(Integer.parseInt(attributes.getValue(1);this.tagName=qName;/调用多次
9、 Overridepublic void endElement(String uri, String localName, String qName)throws SAXException if(qName.equals(“student“)this.list.add(this.student);this.tagName=null;/只调用一次Overridepublic void endDocument() throws SAXException /调用多次Overridepublic void characters(char ch, int start, int length)throws
10、 SAXException if(this.tagName!=null)String date=new String(ch,start,length);if(this.tagName.equals(“name“)this.student.setName(date);else if(this.tagName.equals(“sex“)this.student.setSex(date);else if(this.tagName.equals(“age“)this.student.setAge(Integer.parseInt(date);else if(this.tagName.equals(“e
11、mail“)this.student.setEmail(date);else if(this.tagName.equals(“birthday“)this.student.setBirthday(date);else if(this.tagName.equals(“memo“)this.student.setMemo(date);public static void main(String args) SAXParser parser = null;try /构建 SAXParserparser = SAXParserFactory.newInstance().newSAXParser();/
12、实例化 DefaultHandler 对象SaxParseXml parseXml=new SaxParseXml();/加载资源文件 转化为一个输入流InputStream stream=SaxParseXml.class.getClassLoader().getResourceAsStream(“Student.xml“);/调用 parse()方法parser.parse(stream, parseXml);/遍历结果http:/ / 13List list=parseXml.getList();for(Student student:list)System.out.println(“i
13、d:“+student.getId()+“tgroup:“+student.getGroup()+“tname:“+student.getName()+“tsex:“+student.getSex()+“tage:“+student.getAge()+“temail:“+student.getEmail()+“tbirthday:“+student.getBirthday()+“tmemo:“+student.getMemo(); catch (ParserConfigurationException e) e.printStackTrace(); catch (SAXException e)
14、 e.printStackTrace(); catch (IOException e) e.printStackTrace();效果图:Pull 方式解析 xml依赖的环境kxml2-2.3.0.jarxmlpull_1_1_3_4c.jar以上两个 jar 分别从以下网站下载:http:/www.xmlpull.org/ xmlpullhttp:/ kxmlxml 文件 :张三男1987-06-08好学生李四女1987-06-08好学生小王男1987-06-08好学生小张男1987-06-08好学生小明男1987-06-08好学生http:/ / 13Xml 对应的实体 bean 文件:pa
15、ckage com.parsexml.entity;public class Student private int id;private int group;private String name;private String sex;private int age;private String email;private String memo;private String birthday;public int getId() return id;public void setId(int id) this.id = id;public int getGroup() return gro
16、up;public void setGroup(int group) this.group = group;public String getName() return name;public void setName(String name) this.name = name;public String getSex() return sex;public void setSex(String sex) this.sex = sex;public int getAge() return age;public void setAge(int age) this.age = age;public
17、 String getEmail() return email;public void setEmail(String email) this.email = email;public String getMemo() return memo;public void setMemo(String memo) this.memo = memo;public String getBirthday() return birthday;public void setBirthday(String birthday) this.birthday = birthday;Pull 解析代码package c
18、om.parsexml;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;import com.parsexml.entity.Student;public class PullParseXml public List PullParseXML()L
19、ist list=null;http:/ / 13Student student = null;/构建 XmlPullParserFactorytry XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance();/获取 XmlPullParser 的实例XmlPullParser xmlPullParser=pullParserFactory.newPullParser();/设置输入流 xml 文件xmlPullParser.setInput(Thread.currentThread().getConte
20、xtClassLoader().getResourceAsStream(“Student.xml“), “UTF-8“);/开始int eventType=xmlPullParser.getEventType();try while(eventType!=XmlPullParser.END_DOCUMENT)String nodeName=xmlPullParser.getName();switch (eventType) /文档开始case XmlPullParser.START_DOCUMENT:list=new ArrayList();break;/开始节点case XmlPullPar
21、ser.START_TAG:/判断如果其实节点为 studentif(“student“.equals(nodeName)/实例化 student 对象student=new Student();/设置 Id 属性student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0);/设置 Group 属性student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1);else if(“name“.equals(nodeName)/设置 namestudent
22、.setName(xmlPullParser.nextText();else if(“sex“.equals(nodeName)/设置 sexstudent.setSex(xmlPullParser.nextText();else if(“age“.equals(nodeName)/设置 agestudent.setAge(Integer.parseInt(xmlPullParser.nextText();else if(“email“.equals(nodeName)/设置 emailstudent.setEmail(xmlPullParser.nextText();else if(“bir
23、thday“.equals(nodeName)/设置 birthdaystudent.setBirthday(xmlPullParser.nextText();else if(“memo“.equals(nodeName)/设置 memo 属性student.setMemo(xmlPullParser.nextText();break;/结束节点case XmlPullParser.END_TAG:if(“student“.equals(nodeName)list.add(student);student=null;break;default:break;eventType=xmlPullPa
24、rser.next(); catch (NumberFormatException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); catch (XmlPullParserException e) e.printStackTrace();return list;public static void main(String args) List list=new PullParseXml().PullParseXML();http:/ / 13for(Student student:list)System.ou
25、t.println(“id:“+student.getId()+“tgroup:“+student.getGroup()+“tname:“+student.getName()+“tsex:“+student.getSex()+“tage:“+student.getAge()+“temail:“+student.getEmail()+“tbirthday:“+student.getBirthday()+“tmemo:“+student.getMemo();注:若工程中缺少 kxml2-2.3.0.jar 文件则会报以下异常信息:你应该添加 kxml2-2.3.0.jar 包到你的 classpath 下效果图: