-- 作者:mfc42d
-- 发布时间:9/14/2004 5:34:00 PM
-- 一个使用sax的简单例子
book.xml <?xml version="1.0" encoding="UTF-8"?> <book xmlns="http://sauria.com/schemas/apache-xml-book/book" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sauria.com/schemas/apache-xml-book/book http://www.sauria.com/schemas/apache-xml-book/book.xsd" version="1.0"> <title>Professional XML Development with Apache Tools</title> <author>Theodore W. Leung</author> <isbn>0-7645-4355-5</isbn> <month>December</month> <year>2003</year> <publisher>Wrox</publisher> <address>Indianapolis, Indiana</address> </book> Book.java package com.sauria.apachexml.ch1; public class Book { String title; String author; String isbn; String month; int year; String publisher; String address; public String getAddress() { return address; } public String getAuthor() { return author; } public String getIsbn() { return isbn; } public String getMonth() { return month; } public String getPublisher() { return publisher; } public String getTitle() { return title; } public int getYear() { return year; } public void setAddress(String string) { address = string; } public void setAuthor(String string) { author = string; } public void setIsbn(String string) { isbn = string; } public void setMonth(String string) { month = string; } public void setPublisher(String string) { publisher = string; } public void setTitle(String string) { title = string; } public void setYear(int i) { year = i; } public String toString() { return title + " by " + author; } } BookHandler.java package com.sauria.apachexml.ch1; import java.util.Stack; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; public class BookHandler extends DefaultHandler { private Stack elementStack = new Stack(); private Stack textStack = new Stack(); private StringBuffer currentText = null; private Book book = null; public Book getBook() { return book; } public void startElement( String uri, String localPart, String qName, Attributes attributes) throws SAXException { currentText = new StringBuffer(); textStack.push(currentText); elementStack.push(localPart); if (localPart.equals("book")) { String version = attributes.getValue("", "version"); if (version != null && !version.equals("1.0")) throw new SAXException("Incorrect book version"); book = new Book(); } } public void endElement(String uri, String localPart, String qName) throws SAXException { String text = textStack.pop().toString(); if (localPart.equals("book")) { } else if (localPart.equals("title")) { book.setTitle(text); } else if (localPart.equals("author")) { book.setAuthor(text); } else if (localPart.equals("isbn")) { book.setIsbn(text); } else if (localPart.equals("month")) { book.setMonth(text); } else if (localPart.equals("year")) { int year; try { year = Integer.parseInt(text); } catch (NumberFormatException e) { throw new SAXException("year must be a number"); } book.setYear(year); } else if (localPart.equals("publisher")) { book.setPublisher(text); } else if (localPart.equals("address")) { book.setAddress(text); } else { throw new SAXException("Unknown element for book"); } elementStack.pop(); } public void characters(char[] ch, int start, int length) throws SAXException { currentText.append(ch, start, length); } public void warning(SAXParseException ex) throws SAXException { System.err.println( "[Warning] " + getLocationString(ex) + ": " + ex.getMessage()); } public void error(SAXParseException ex) throws SAXException { System.err.println( "[Error] " + getLocationString(ex) + ": " + ex.getMessage()); } public void fatalError(SAXParseException ex) throws SAXException { System.err.println( "[Fatal Error] " + getLocationString(ex) + ": " + ex.getMessage()); throw ex; } /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); } } SAXMain.java package com.sauria.apachexml.ch1; import java.io.IOException; import org.apache.xerces.parsers.SAXParser; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; public class SAXMain { public static void main(String[] args) { XMLReader r = new SAXParser(); BookHandler bookHandler = new BookHandler(); r.setContentHandler(bookHandler); r.setErrorHandler(bookHandler); try { r.parse("book.xml"); System.out.println(bookHandler.getBook().toString()); System.out.println(bookHandler.getBook().getAddress().toString()); } catch (SAXException se) { System.out.println("SAX Error during parsing " + se.getMessage()); se.printStackTrace(); } catch (IOException ioe) { System.out.println("I/O Error during parsing " + ioe.getMessage()); ioe.printStackTrace(); } catch (Exception e) { System.out.println("Error during parsing " + e.getMessage()); e.printStackTrace(); } } }
|