SAX-xml解析

Posted leigepython

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SAX-xml解析相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治马丁</author>
</book>
<book id="2">
<name>安徒生童话</name>
<author>2004</author>
</book>
</bookstore>

package com.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * SAX 包来解析xml simple apis for xml
 */

public class demo01 {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        MyHandle myHandle = new MyHandle();
        saxParser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/xml/index.xml"), myHandle);
        List<User> users = myHandle.getUsers();
        for (User item : users) {
            System.out.println(item.getName() + "----------------" + item.getAuthor());
        }
    }
}

class MyHandle extends DefaultHandler {
    private List<User> users = new ArrayList<>();
    private String tag;
    private User user;

    public MyHandle() {
        super();
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if (null != qName) {
            tag = qName;
        }
        if (null != qName && qName.equals("book")) {
            this.user = new User();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if (null != qName && qName.equals("book")) {
            if (null != user) {
                this.users.add(this.user);
            }
        }
        this.tag = null;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String line = new String(ch, start, length).trim();
        // 如果是名字标签
        if (null != tag && tag.equals("name")) {
            user.setName(line);
        } else if (null != tag && tag.equals("author")) {
            user.setAuthor(line);
        }
    }
}

  

package com.xml;

public class User {
    private String name;
    private String author;
    public static void main(String[] args){
    }

    public User() {
    }

    public User(String name, String author) {
        this.name = name;
        this.author = author;
    }

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

走过的坑,没有实例化范型类














以上是关于SAX-xml解析的主要内容,如果未能解决你的问题,请参考以下文章

JavaSE XML解析技术的使用详解

XML解析

XML的XmlPullParser解析

XML 解析---dom解析和sax解析

XML解析——Java中XML的四种解析方式

XML文件解析DOM解析和SAX解析