book.xml(需要解析的XML文档)
- <?xml version="1.0" encoding="UTf-8">
- <books>
- <book bookno="001">
- <title>大数据时代</title>
- <author>张三</author>
- <price>50</price>
- </book>
- <book bookno="002">
- <title>Andriod开发</title>
- <author>李四</author>
- <price>70</price>
- </book>
- </books>
Book.java(文档的构造)
- package bo;
- public class Book {
- public String bookno;
- public String title;
- public String author;
- public double price;
- public String getBookno() {
- return bookno;
- }
- public void setBookno(String bookno) {
- this.bookno = bookno;
- }
- 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 double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- @Override
- public String toString() {
- return "Book [author=" + author + ",bookno=" + bookno + ",price=" + price
- + ",title=" + title + "]";
- }
- public Book() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Book(String bookno,String title,String author,double price) {
- super();
- this.bookno = bookno;
- this.title = title;
- this.author = author;
- this.price = price;
- }
- }
XmlParse.java(接口类,供SaxParse调用)
- package bo;
- import java.util.List;
- public interface XmlParse
- {
- public List<Book> xmlparse(String fileName);
- }
Mysax.java(解析的功能实现类)
- package sax;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import bo.Book;
- public class Mysax extends DefaultHandler {
- private String tag;
- private Book book;
- private List<Book> list;
- public List<Book> getList()
- {
- return list;
- }
- @Override
- public void characters(char[] arg0,int arg1,int arg2) throws SAXException {
- // TODO Auto-generated method stub
- super.characters(arg0,arg1,arg2);
- if(tag!=null)
- {
- String string=new String(arg0,arg2).trim();
- if(tag.equals("tital"))
- {
- book.setTitle(string);
- }
- if(tag.equals("author"))
- {
- book.setAuthor(string);
- }
- if(tag.equals("price"))
- {
- book.setPrice(Double.parseDouble(string));
- }
- }
- }
- @Override
- public void endDocument() throws SAXException {
- // TODO Auto-generated method stub
- super.endDocument();
- }
- @Override
- public void endElement(String arg0,String arg1,String arg2)
- throws SAXException {
- // TODO Auto-generated method stub
- super.endElement(arg0,arg2);
- if(arg2.equals("book"))
- {
- list.add(book);
- book=null;//为下一步解析做准备
- }
- tag=null;
- }
- @Override
- public void startDocument() throws SAXException {
- // TODO Auto-generated method stub
- super.startDocument();
- list=new ArrayList<Book>();
- }
- @Override
- public void startElement(String arg0,String arg2,Attributes arg3) throws SAXException {
- // TODO Auto-generated method stub
- super.startElement(arg0,arg2,arg3);
- if(arg2.equals("book"))
- {
- book=new Book();
- String bookNo=arg3.getValue("bookno");
- book.setBookno(bookNo);
- }
- tag=arg2;
- }
- }
SaxParse.java(解析主类,调用其他类,实现其解析功能)
- package sax;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.text.html.parser.Parser;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.SAXException;
- import bo.Book;
- import bo.XmlParse;
- public class SaxParse implements XmlParse {
- public List<Book> xmlparse(String fileName) {
- List<Book> list=new ArrayList<Book> ();//利用集合装载对象进行解析处理
- SAXParserFactory factory=SAXParserFactory.newInstance();//建立Sax解析工程对象
- Mysax handler=null;
- try {
- SAXParser parser=factory.newSAXParser();//建立解析对象
- InputStream is=new FileInputStream(fileName);
- handler=new Mysax();
- parser.parse(is,handler);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ParserConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SAXException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- list=handler.getList();
- return list;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- XmlParse parse=new SaxParse();
- List<Book> list=parse.xmlparse("book.xml");
- System.out.println("Sax 解析结果:");
- for(Book book:list)
- {
- System.out.println(book);
- }
- }
- }