風行草偃

陪伴,是最长情的告白。

XML SAX解析方式

Saoirse:

一、SAX解析步骤


获取解析工厂


SAXParserFactory factory = SAXParserFactory.newInstance();


从解析工厂获取解析器


SAXParser parser = factory.newSAXParser();


加载文档Document注册处理器


parser.parse(Thread.currentThread().getContextClassLoader().
        getResourceAsStream("cn/edu/dhu/person.xml"), handler);


编写处理器


CustomHandler handler = new CustomHandler();


二、示例代码


1.主程序


package cn.edu.dhu;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;

public class XMLParse {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();

        CustomHandler handler = new CustomHandler();
        parser.parse(Thread.currentThread().getContextClassLoader().
                getResourceAsStream("cn/edu/dhu/person.xml"), handler);

        List<Person> persons = handler.getPersons();
        for(Person p:persons){
            System.out.println(p.getName() + "--" + p.getAge());
        }
    }
}


2. 处理器代码


package cn.edu.dhu;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
public class CustomHandler extends DefaultHandler {
        private List<Person> persons;
        private Person person;
        private String tag;

        // 开始处理文档
        @Override
        public void startDocument() throws SAXException {
            persons = new ArrayList<Person>();
        }

        // 文档处理结束
        @Override
        public void endDocument() throws SAXException {
        }

        // 开始处理一个元素
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if (null != qName) {
                tag = qName;
            }
            if (null != qName && qName.equals("person")) {
                person = new Person();
            }
        }

        // 结束一个元素处理
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
             if (qName.equals("person")) {
                this.persons.add(person);
            }
            tag = null;
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            String str = new String(ch, start, length);
                if (null!=tag && tag.equals("name")) {
                person.setName(str);
            } else if (null != tag && tag.equals("age")) {
                Integer age = Integer.valueOf(str);
                person.setAge(age);
            }
        }

        public List<Person> getPersons() {
            return persons;
        }
}


3. Person类


package cn.edu.dhu;
public class Person {
        private String name;
        private int age;


        public Person() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
}


4.person.xml文件


<?xml version="1.0" encoding="utf-8" ?>
<persons>
    <person>
        <name>张三</name>
        <age>30</age>
    </person>
    <person>
        <name>李四</name>
        <age>25</age>
    </person>
    <person>
        <name>老王</name>
        <age>66</age>
    </person>
</persons>


店铺:https://shop58616120.taobao.com



评论
热度 ( 1 )
  1. 風行草偃Saoirse 转载了此文字

© 風行草偃 | Powered by LOFTER