SAX采用的是事件驅動模型。需要先定義一些方法,解析器解析時,當觸發事件時,會自動調用響應的已經定義好的方法。一般要重寫的方法有:
Start document //開始解析時的事件
startElement(String uri, String localName, String qName, Attributes attr) //遇到一個元素的開始時的事件
characters( char[] chars, int start, int length ) //遇到的文本。start表示文本在數組chars中的開始位置,length表示文本的長度
endElement(String uri, String localName, String qName) //遇到元素結束標記時的事件
對于下面的XML
<?xml version="1.0" encoding="gb2312"?>
<student>
<person age="25"><!--如果沒有age屬性,默認的為20-->
<name>崔衛兵</name>
<college>PC學院</college>
<telephone>62354666</telephone>
<notes>男,1982年生,碩士,現就讀于北京郵電大學</notes>
</person>
<person>
<name>cwb</name>
<college leader="leader1">PC學院</college><!--如果沒有leader屬性,默
認的為leader-->
<telephone>62358888</telephone>
<notes>男,1987年生,碩士,現就讀于中國農業大學</notes>
</person>
<person age="45">
<name>xxxxx</name>
<college leader="學院領導">xxx學院</college>
<telephone>66666666</telephone>
<notes>注視中,注釋中</notes>
</person>
</student>
通過下面的程序解析后,可以把每個標簽中的內容提取出
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import java.io.*;


public class TestSAX extends DefaultHandler
{


public TestSAX()
{
super();
}

public void startElement(String uri, String localName, String qName, Attributes atts)throws SAXException
{

/**//*if(qName.equalsIgnoreCase("person")){
String age=atts.getValue("age");
if(age!=null)
System.out.println("age: "+age);
else System.out.println("age: 20");
}
else if(qName.equalsIgnoreCase("name")){
tagName="name";
}
else if(qName.equalsIgnoreCase("college")){
tagName="college";
}
else if(qName.equalsIgnoreCase("telephone")){
tagName="telephone";
}
else if(qName.equalsIgnoreCase("notes")){
tagName="notes";
}*/

if("person".equals(qName))
{
String age=atts.getValue("age");
if(age!=null)
System.out.println("age: "+age);
else System.out.println("age: 20");
}
if("name".equals(qName) || "college".equals(qName) || "telephone".equals(qName) || "notes".equals(qName))
tagName=qName;
}

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

/**//*if(tagName.equals("name")) System.out.println("name: "+text);
else if(tagName.equals("college")) System.out.println("college: "+text);
else if(tagName.equals("telephone")) System.out.println("telephone: "+text);
else if(tagName.equals("notes")) System.out.println("notes: "+text);*/

if("name".equals(tagName))
{
System.out.println("name: "+text);
}

else if("college".equals(tagName))
{
System.out.println("college: "+text);
}

else if("telephone".equals(tagName))
{
System.out.println("telephone: "+text);
}

else if("notes".equals(tagName))
{
System.out.println("notes: "+text);
}
tagName=null;
}
private String tagName=null;

public static void main(String[] args)
{
// TODO Auto-generated method stub

try
{
TestSAX testSAX=new TestSAX();
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
parser.parse(new File("E:/我的文檔/tmp/test.xml"), testSAX);
}

catch(IOException e)
{
e.printStackTrace();

}catch(SAXException e)
{
e.printStackTrace();

}catch(Exception e)
{
e.printStackTrace();
}
}

}

注意程序中注釋的內容,它本來與外面的代碼按說是等價的,但是如果用注釋里面的代碼就會出錯?;蛟S是因為當參數為null時,也會觸發startElement和characters事件,這樣,在空的string對象上調用equals就會出錯