SAX解析

SaxParseTest.java

package cn.pinnsvin;

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

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import cn.pinnsvin.bean.Book;


//sax解析xml
public class SaxParseTest {

    @Test
    public void saxParse() throws ParserConfigurationException, SAXException, IOException{

        //获取工厂
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();

        //获取xmlReader,通过这个reader可以处理ContentHandler
        XMLReader reader = saxParser.getXMLReader();
        //给XMLReader设置contentHandler,ContentHandler是一个接口,里面有太多方法没有实现
        //不去实现ContentHandler,而是继承它默认的实现DefaultHandler
        reader.setContentHandler(new MyHandler());
        //解析xml文档
        reader.parse("book.xml");

    }
    private class MyHandler extends DefaultHandler{

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            System.out.println("文档内容:"+(new String(ch,start,length)));
        }

        @Override
        public void endDocument() throws SAXException {
            System.out.println("文档结束");
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            System.out.println("结束标签:+qName+">");
        }

        @Override
        public void startDocument() throws SAXException {
            System.out.println("文档开始");
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            System.out.println("标签开始<"+qName+">");
        }

    }

    @Test
    public void saxParse2() throws ParserConfigurationException, SAXException, IOException{
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(new DefaultHandler(){

            private boolean isPrice=false;
            private int count=0;

            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                if(isPrice&&count==2){
                    System.out.println(new String(ch,start,length));
                }
            }

            @Override
            public void endElement(String uri, String localName, String qName)
                    throws SAXException {
                if("价格".equals(qName)){
                    isPrice=false;
                }
            }

            @Override
            public void startElement(String uri, String localName,
                    String qName, Attributes attributes) throws SAXException {
                if("价格".equals(qName)){
                    isPrice = true;
                    count++;
                }
            }

        });
        xmlReader.parse("book.xml");
    }

//解析xml文档,并将其转换成ArrayList
    @Test
    public void saxParseToList() throws ParserConfigurationException, SAXException, IOException{

        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(new DefaultHandler(){

            private ArrayList bookSheif = null;
            private Book book;
            String tag = null;
            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                if("书名".equals(tag)){
                    book.setTitle(new String(ch,start,length));
                }else if("作者".equals(tag)){
                    book.setAuthor(new String(ch,start,length));
                }else if("价格".equals(tag)){
                    book.setPrice(new String(ch,start,length));
                }
            }

            @Override
            public void endElement(String uri, String localName, String qName)
                    throws SAXException {
                tag = null;
                if("书架".equals(qName)){
                    for (Book book1 : bookSheif) {
                        System.out.println(book1);
                    }
                }else if("书".equals(qName)){
                    bookSheif.add(book);
                }
            }

            @Override
            public void startElement(String uri, String localName,
                    String qName, Attributes attributes) throws SAXException {
                if("书架".equals(qName)){
                    //创建ArrayList
                    bookSheif = new ArrayList();
                }else if("书".equals(qName)){
                    //创建对象
                    book = new Book();
                }else if("书名".equals(qName)){
                    tag = "书名";
                }else if("作者".equals(qName)){
                    tag = "作者";
                }else if("价格".equals(qName)){
                    tag = "价格";
                }
            }

        });
        xmlReader.parse("book.xml");
    }
}

book.java

package cn.pinnsvin.bean;

public class Book {
    private String title;
    private String author;
    private String price;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [title=" + title + ", author=" + author + ", price="
                + price + "]";
    }

}

book.xml

<书架>
    <>
        <书名>java深入浅出书名>
        <作者>你猜 作者>
        <价格>20.50价格>
    >
    <>
        <书名>c深入浅出书名>
        <作者>haha 作者>
        <价格>99.00价格>
    >
书架>

你可能感兴趣的:(java)