java中使用SAX读写XML文件

SAX 是 Simple API for XML 的简写。虽说simple,用起来却比DOM麻烦,不过麻烦是有性能作为回报的。

SAX不会一次组织一整棵XML文档的抽象树给你使用,而是看到什么就问一下你要不要执行什么,这就是所谓的“基于事件”。

XML文件继续使用DOM和JDOM中的例子,当SAX解析器(实际上好像不应这么叫)读取文件,遇到“”元素的时候,它知道这是文档开头,于是问你要不要干些什么;然后继续读取XML文档,当遇到“”元素时,它又会问你要不要干些什么... 直到整个XML

文件被读取完毕,不过这时它还会问你,“文档读完了,你要不要干些什么呢?”...

关于这个过程,较为专业的说法是:

SAX 分析经过其的 XML 流,这非常象老式的自动收报机纸条。考虑以下 XML 代码片断:Java 代码

1.

2.

3.UNIX

4.color

5.

version="1.0"?>UNIXcolor

一般情况下,SAX 处理器分析这段代码将生成以下事件:

Start document

Start element (samples)

Characters (white space)

Start element (server)

Characters (UNIX)

End element (server)

Characters (white space)

Start element (monitor)

Characters (color)

End element (monitor)

Characters (white space)

End element (samples)

实例:

//BookBean.java

import java.util.*;

public class BookBean implements Comparable{ private String title = "";

private String author = "";

private String url = "";

private String day ="";

private String month ="";

private String year ="";

public String getAuthor() {

return author;

}

public void setAuthor(String author) { this.author = author;

}

public String getDay() {

return day;

}

public void setDay(String day) {

this.day = day;

}

public String getMonth() {

return month;

}

public void setMonth(String month) {

this.month = month;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getYear() {

return year;

}

public void setYear(String year) {

this.year = year;

}

public String getDate(){

return this.day+"-"+this.month+"-"+this.year;

}

public int compareTo(Object obj){

BookBean bb=(BookBean)obj;

return https://www.360docs.net/doc/019768864.html,pareTo(bb.getTitle());

}

}

//MySAX.java

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.*;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import java.util.*;

public class MySAX extends DefaultHandler{

private Hashtable table = null;

private BookBean book = null;

private String value = "";

public MySAX(){

System.out.println("invoke MySAX().");

table = new Hashtable();

}

public void open(String xmlfile){

try{

SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false);

SAXParser parser = spf.newSAXParser();

XMLReader myreader = parser.getXMLReader();

myreader.setContentHandler(this);

InputSource source = new InputSource(xmlfile);

myreader.parse(source);

}catch(Exception e){

System.out.println("an error caused.");

}

}

public static void main(String args[]){

System.out.println("main.");

MySAX ms = new MySAX();

ms.open("./books.xml");

Hashtable table = ms.getTable();

Enumeration enu = table.keys();

ArrayList al=new ArrayList();

while(enu.hasMoreElements()){

String title = (String)enu.nextElement();

BookBean book = (BookBean)table.get(title);

al.add(book);

}

Collections.sort(al);

for(BookBean book:al){

System.out.println(book.getTitle());

System.out.println(book.getAuthor());

System.out.println(book.getUrl());

System.out.println(book.getDate());

}

}

public void startDocument() throws SAXException{

System.out.println("startDocument");

}

public void startElement(String namespaceURI,String localName,String qName,Attributes attrs) throws SAXException{

if(qName.equals("book")){

/*if(book!=null){

table.put(book.getTitle(),book);

}

else{

book = new BookBean();

System.out.println("****************"+table.size());

}*/

book = new BookBean();

}

}

public void endElement(String namespaceURI,String localName,String qName)

throws SAXException{

if(qName.equals("title"))

book.setTitle(value);

if(qName.equals("author"))

book.setAuthor(value);

if(qName.equals("url"))

book.setUrl(value);

if(qName.equals("day"))

book.setDay(value);

if(qName.equals("month"))

book.setMonth(value);

if(qName.equals("year"))

book.setYear(value);

if(qName.equals("book")){

table.put(book.getTitle(),book);

System.out.println("****************"+table.size());

}

}

public void characters(char ch[],int start,int length) throws SAXException{ value = new String(ch,start,length);

}

public void endDocument() throws SAXException{

//table.put(book.getTitle(),book);

}

public Hashtable getTable(){

return this.table;

}

}

//books.xml你要读取的xml文件

one

zmhot

https://www.360docs.net/doc/019768864.html,

2010

8

24

two

zmhot

https://www.360docs.net/doc/019768864.html, 2010

8

24

three

zmhot

https://www.360docs.net/doc/019768864.html, 2010

8

24

four

zmhot

https://www.360docs.net/doc/019768864.html, 2010

8

24

five

zmhot

https://www.360docs.net/doc/019768864.html, 2010

8

24

six

zmhot

https://www.360docs.net/doc/019768864.html, 2010

8

24

相关文档
最新文档