SAX解析XML文件
来源:互联网 发布:unity3d圣典app 编辑:程序博客网 时间:2024/05/22 02:12
XML是可扩展标记语言,可以使用SAX或者PULL解析。今天所写的是使用SAX解析XML文件。 SAX是Simple API for XML的缩写,几乎所有的XML解析器都会支持它。
使用SAX解析的步骤:
1, 创建SAX 解析器的工厂
2, 通过解析器工厂 创建 SAX解析器
3, 自定义解析的处理器 MyHandler 继承 DefaultHandler类
4, 解析xml文档
5, 需要通过handler对象调用方法 , 获取解析的结果。
解析实例:
xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <students>
- <student >
- <id>1</id>
- <name>zhangsan</name>
- <age>20</age>
- <sex>male</sex>
- </student>
- <student >
- <id>2</id>
- <name>lisi</name>
- <age>21</age>
- <sex>female</sex>
- </student>
- </students>
对应的Student类:- public class Student {
- private int id;
- private String name;
- private String sex;
- private int age;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- 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;
- }
-
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
- }
-
- }
- public class MyHandler extends DefaultHandler {
-
-
- private ArrayList<Student> list;
- private String tag;
-
-
- private Student student;
-
- public ArrayList<Student> getList() {
- return list;
- };
-
- @Override
- public void startDocument() throws SAXException {
-
- list = new ArrayList<>();
-
- }
-
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
-
-
-
-
- tag = qName;
-
-
- if ("student".equals(qName)) {
- student = new Student();
- }
- }
-
- @Override
- public void characters(char[] ch, int start, int length) throws SAXException {
-
-
-
- String content = new String(ch, start, length).trim();
-
- if ("id".equals(tag)) {
- student.setId(Integer.parseInt(content));
-
- } else if ("name".equals(tag)) {
- student.setName(content);
- } else if (("age").equals(tag)) {
- student.setAge(Integer.parseInt(content));
- } else if ("sex".equals(tag)) {
- student.setSex(content);
- }
-
- }
-
- @Override
- public void endElement(String uri, String localName, String qName) throws SAXException {
-
-
-
-
-
-
- tag = "";
-
-
- if ("student".equals(qName)) {
- list.add(student);
- }
-
- }
-
- @Override
- public void endDocument() throws SAXException {
-
- super.endDocument();
- }
- }
- public class Test1 {
- public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
-
- SAXParserFactory factory = SAXParserFactory.newInstance();
-
- SAXParser parser = factory.newSAXParser();
-
- MyHandler mh = new MyHandler();
-
- parser.parse("XML文件路径", mh);
-
- ArrayList<Student> list = mh.getList();
- for (Student student : list) {
- System.out.println(student);
- }
-
- }
- }
但是如果遇见XML文件是:
- <?xml version="1.0" encoding="UTF-8"?>
- <students>
- <student id="1">
- <name>zhangsan</name>
- <age>20</age>
- <sex>male</sex>
- </student>
- <student id="2">
- <name>lisi</name>
- <age>21</age>
- <sex>female</sex>
- </student>
- </students>
需要将MyHandler类修改为:- public class MyHandler extends DefaultHandler {
-
-
- private ArrayList<Student> list;
- public ArrayList<Student> getList()
- {
- return list;
- }
-
- private String tag;
-
- private Student student;
-
- @Override
- public void startDocument() throws SAXException {
-
- list = new ArrayList<>();
- }
-
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
-
- tag = qName;
-
-
- if("student".equals(qName))
- {
- student = new Student();
-
- if(null !=attributes)
- {
-
- int count = attributes.getLength();
- for(int i=0;i<count;i++)
- {
-
- String name = attributes.getQName(i);
-
- String value = attributes.getValue(i);
-
- if("id".equals(name))
- {
- student.setId(Integer.parseInt(value));
- }
- }
-
- }
- }
-
-
- }
-
- @Override
- public void characters(char[] ch, int start, int length) throws SAXException {
-
-
- String content = new String(ch,start,length).trim();
-
- if("name".equals(tag))
- {
- student.setName(content);
- }else if("age".equals(tag))
- {
- student.setAge(content);
- }else if("sex".equals(tag))
- {
- student.setSex(content);
- }
- }
-
-
- @Override
- public void endElement(String uri, String loaclName, String qName)
- throws SAXException {
-
- tag = "";
-
- if("student".equals(qName))
- {
- list.add(student);
- }
-
- }
-
-
- @Override
- public void endDocument() throws SAXException {
-
- super.endDocument();
- }
- }
0 0