欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

XML SAX解析

程序员文章站 2022-06-16 15:45:38
...

XML SAX解析
XML SAX解析

person.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persons>
    <person>
        <name>至尊宝</name>
        <age>9000</age>
    </person>
    <person>
        <name>白晶晶</name>
        <age>7000</age>
    </person>
</persons>




person.java
package com.bjsxt.xml;
public class Person {
    private String name;
    private int age;

    public Person() {
        // TODO Auto-generated constructor stub
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    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;
    }
}



PersonHandler.java
package com.bjsxt.xml;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * 存储对象
 * @author Administrator
 */
public class PersonHandler extends DefaultHandler {
    private List<Person> persons;
    private Person person;
    private String tag;//记录标签名
    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        //System.out.println("处理文档开始");
        persons =new ArrayList<Person>();
    }
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        System.out.println("开始一个元素" +qName);
        if(null!=qName){
            tag=qName;
        }
        if(null!=qName &&qName.equals("person")){
            person =new Person();
        }
    }
    @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")){
            //System.out.println(new String(ch,start,length));
            person.setName(str);
        }else if(null!=tag &&tag.equals("age")){
            Integer age = Integer.valueOf(str);
            person.setAge(age);
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        //System.out.println("结束一个元素" +qName);
        if(qName.equals("person")){
            this.persons.add(person);
        }
        tag =null;
    }
    @Override
    public void endDocument() throws SAXException {
        System.out.println("文档处理结束");
    }
    public List<Person> getPersons() {
        return persons;
    }
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }   
}




ParseDemo01.java
package com.bjsxt.xml;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
 *解析
 */
public class ParseDemo01 {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        //1、获取解析工厂
        SAXParserFactory factory=SAXParserFactory.newInstance();
        //2、从解析工厂获取解析器
        SAXParser parse =factory.newSAXParser();
        //3、加载文档 Document 注册处理器
        //4、编写处理器
        PersonHandler handler=new PersonHandler();
        parse.parse(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("com/bjsxt/xml/person.xml")
                ,handler );

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

上一篇: SAX-解析XML

下一篇: XML解析