码迷,mamicode.com
首页 > 其他好文 > 详细

SAX-xml解析

时间:2018-11-21 10:20:13      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:ati   simple   book   util   void   source   之歌   als   conf   

<?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解析

标签:ati   simple   book   util   void   source   之歌   als   conf   

原文地址:https://www.cnblogs.com/leigepython/p/9993209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!