XML解析---SAX

前端之家收集整理的这篇文章主要介绍了XML解析---SAX前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

SAX解析

1、编写SaxHelper 继承 DefaultHandler
1)创建目标类
2)创建ArrayList,用来存放解析结果;
3)复写startDocument() 当SAX解析器解析到XML文档开始时,会调用方法
4)复写endDocument() 当SAX解析器解析到XML文档结束时,会调用方法
5)复写characters() 当SAX解析器解析到某个元素开始时,会调用方法 其中localName记录的是元素属性
6)复写endElement()当SAX解析器解析到某个元素结束时,会调用方法 其中localName记录的是元素属性

2、编写sax()
1)创建ArrayList,用来存放解析结果;
2)把xml文件转化为InputSource格式;
3)通过SAXParserFactory.newInstance() 获取factory;
4)通过factory获得saxParser;
5)通过saxParser.getXMLReader() 获得xml阅读器;
6)设置 阅读器 handler xmlReader.setContentHandler;
7)设置 阅读器的InputSource

## BOOK类 ##
package com.example.xmltest;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException
public class Book {

    private String id;
    private String name;
    private String prize;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPrize() {
        return prize;
    }
    public void setPrize(String prize) {
        this.prize = prize;
    }
    public String toString()
    {
        return this.id + this.name + this.prize ;
    }



}

待读取xml

<?xml version="1.0" encoding="utf-8"?>  
<books>  
    <book auther="a">  
        <id>1001</id>  
        <name>Thinking In Java</name>  
        <price>80.00</price>  
    </book>  
    <book auther="b">  
        <id>1002</id>  
        <name>Core Java</name>  
        <price>90.00</price>  
    </book>  
    <book auther="c">  
        <id>1003</id>  
        <name>Hello,Andriod</name>  
        <price>100.00</price>  
    </book>  
</books>

SAXHelper

package com.example.xmltest;

import java.util.ArrayList;
import java.util.List;

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

public class SaxHelper extends DefaultHandler{

    private int currentstate = 0;
    final int id = 1;
    final int name = 2;
    final int price = 3;
    Book book;
    List<Book> books;

    public List getbooks()
    {
        return books;
    }

    @Override
    public void characters(char[] ch,int start,int length)
            throws SAXException {
        // TODO Auto-generated method stub
        String str = new String(ch);
        switch (currentstate) {
        case id:
             book.setId(str);
             currentstate = 0;

            break;
        case name:
             book.setName(str);
             currentstate = 0;

            break;
        case price:
             book.setPrize(str);
             currentstate = 0;

            break;

        default:
            break;
        }

        super.characters(ch,start,length);
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
    }

    @Override
    public void endElement(String uri,String localName,String qName)
            throws SAXException {
        // TODO Auto-generated method stub

        if(localName.equals("book"))
        {
            books.add(book);
        }
        super.endElement(uri,localName,qName);
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        books = new ArrayList<Book>();
        book = new Book();
        super.startDocument();
    }

    @Override
    public void startElement(String uri,String qName,Attributes attributes) throws SAXException {
        if (localName.equals("books"))
        {
            currentstate = 0;
            return;
        }
        if (localName.equals("book"))
        {
            book = new Book();
            return;
        }
        if (localName.equals("id"))
        {
            currentstate = id;
            return;
        }
        if (localName.equals("name"))
        {
            currentstate = name;
            return;
        }
        if (localName.equals("price"))
        {
            currentstate = price;
            return;
        }

        // TODO Auto-generated method stub



        super.startElement(uri,qName,attributes);
    }




}

sax()

private void sax(InputSource inputstream)throws Exception{
        SAXParserFactory factory = SAXParserFactory.newInstance();
        List<Book> books = new ArrayList<Book>();
        try {
            SAXParser  saxparser = factory.newSAXParser();
            //获得阅读器
            XMLReader xmlreader = saxparser.getXMLReader();
            SaxHelper saxh =  new SaxHelper();
            xmlreader.setContentHandler(saxh);
            xmlreader.parse(inputstream);
            //返回数据
            books = saxh.getbooks();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for(Book book:books)
        {
            System.out.println(book.toString());
        }
    }
原文链接:https://www.f2er.com/xml/296735.html

猜你在找的XML相关文章