怎么用Java结合SAX 2.0 解析XML文档(java,sax,xml,编程语言)

时间:2024-05-09 14:38:43 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

    %E6%80%8E%E4%B9%88%E7%94%A8Java%E7%BB%93%E5%90%88SAX+2.0+%E8%A7%A3%E6%9E%90XML%E6%96%87%E6%A1%A3

/*
自从XML真正形成以来(我认为是Org.w3c组织发布XML标准时开始),XML得到了很快的发展,
很多厂商都有推出了自己的XML解析器,如apache的xalan,IBM的xerces,sun的Jdom等,不过这些都是在
基于JAXP(Java api for XML processing)的,从jdk 1.4.0开始的后续j2sdk里都附加了JAXP,这给开发人员
带来了很大的方便,这使得我们在处理一般的XML功能上的问题时不再需要去用第三方的XML处理器了.
随着XML的迅速发展,SAX也从1.0到了现在的2.0了(还是能够和1.0兼容),结构上有了一些较大的变化.

DOM(document object model)每次读取XML节点时都要把它load到内存里来,在文档很大时,就显得很慢了,SAX(simple API for XML),是一个XML解析器的接口,它比DOM更低级一些,它是一种基于事件和回调模式的XML处理方式,因此在解析速度上DOM是没法比的(当要解析的XML文档很大的时更是如此).那么在SAX中事件响应(event)是什么呢 ?我个人认为这一点和swing,AWT中的事件义有点相似的,都有是指在触发某些特定的行为时所做的处理,如:mouse 的click事件等到.这里则是指碰到特定的XML节点的所做的处理,如文档开始(startDocument),文档结束(endDocument),元素开始(startElement)等很多,大家看一下SAX的API中的方法名字就知道有哪些事件了,基本上可以做到见文知义的.在只想分析XML内容(只读),要求高性能,灵活性能够定位错误信息(SAX能够定位错误的行列位置)时,最好用SAX来做.一般情况下SAX是按下面的原理去使用的:
<1>设置事件处理器(SAX 1.0是使用一个通过继承HandlerBase类的实例来设置的,SAX 2.0则是继承DefaultHandler的,还有用XMLReader方式的,在原理上没有很大的区别)
<2>载入要解析的内容
<3>在需要解析的事件方法里(具体参见SAX API文档)加入自己的控制逻辑.
<4>重复<3>直到解析完为止.

在这里我自己写了一个描述电影海报信息的XML文件(file.xml),用SAX2.0写了一个很简单的XML内容阅读器来解析它,和大家交流一下自己的心得.程序在我的机器上经过了测试的(OS: Win2k Advanced Server(English version),
intel pentium cpu, 256M RAM)
*/

import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

class MyXMLReader extends DefaultHandler
{
//Fields
private int index;
private Locator locator;

//Constructor
public MyXMLReader(){
super(); //it must be done !
}

//nain method
public static void main(String[] args){
try{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
MyXMLReader reader = new MyXMLReader();
sp.parse(new Inputsource("film.xml"),reader);
}
catch(Exception e){
e.printStackTrace();
}
}

//Response the startDocument event
public void startDocument() {
System.out.println("n********************************* (:元旦电影海报 :)***********************************n");
}

//Response the startElement event
public void startElement(String uri, String localName, String qName, Attributes attrs){
if( qName.equalsIgnoreCase("film") ){
index ++;
int attrCount = attrs.getLength();
for( int i = 0; i < attrCount; i ++ ){
String attrName = attrs.getQName(i);
if( attrName.equalsIgnoreCase("name") ){
System.out.println("t第" + index + "场,片名:<<" + attrs.getValue(i) + ">>");
}
if( attrName.equalsIgnoreCase("price") ){
System.out.println("t票价:" + attrs.getValue(i) );
}
if( attrName.equalsIgnoreCase("station") ){
System.out.println("t放映地点:" + attrs.getValue(i) );
}
if( attrName.equalsIgnoreCase("time") ){
System.out.println("t放映时间:" + attrs.getValue(i) );
}
if( attrName.equalsIgnoreCase("describtion") ){
System.out.println("t影片简介:" + attrs.getValue(i) );
}
System.out.println();
}
}

//Response the endDocument event
public void endDocument(){
System.out.println("ttttttt------共有" + index + "场电影要放映");
}

//Response the endElement event
public void endElement(String uri, String localName, String qName){
//add your codes if neccessary ...
}

//Print the fata error information
public void fatalError(SAXParseException e){
System.out.println("nFatal error information --&gt");
System.out.println("t" + e.getMessage());
System.out.println("tAt line " + locator.getLineNumber() +
",column " + locator.getColumnNumber());
}

//Print the usual error information
public void error(SAXParseException e){
System.out.println("nUsual error information --&gt");
System.out.println("t" + e.getMessage());
System.out.println("tAt line " + locator.getLineNumber() +
",column " + locator.getColumnNumber());
}

//Print the warning information
public void warning(SAXParseException e){
System.out.println("nWarning information --&gt");
System.out.println("t" + e.getMessage());
System.out.println("tAt line " + locator.getLineNumber() +
",column " + locator.getColumnNumber());
}

//Store the error locator object
public void setDocumentLocator(Locator lct){
locator = lct;
}

}//End class MyXMLReader

附: film.xml完全的内容:


<!-- 2003年元月1号长沙市各大影院落放映列表 --&gt

<film name="英雄" price="30" station="田汉大剧场" time="19:00"
describtion="国产最新大片,张艺谋导演,梁朝伟,张曼玉,李连杰等众多大明星主演">

<film name="无间道" price="20" station="长沙市演艺中心" time="15:00"
describtion="韩国大片">

<film name="武士" price="20" station="湖南省电影院" time="17:00"
describtion="韩国大片,有点像英雄">

<film name="长排山之战" price="15" station="长沙市电影超市A1厅" time="19:00"
describtion="反映对越自卫反击战时期中国军人的故事片">

<film name="高山下的花环" price="15" station="长沙市电影超市A2厅" time="19:00"
describtion="反映对越自卫反击战时期中国军人的故事片">

<film name="这里的黎明静悄悄" price="15" station="长沙市电影超市A3厅" time="19:00"
describtion="反映对越自卫反击战时期中国军人的故事片">

<film name="子夜" price="15" station="长沙市电影超市B1厅" time="19:00"
describtion="反映对越自卫反击战时期中国军人的故事片">

本文:怎么用Java结合SAX 2.0 解析XML文档的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:SQL语言的查询方法有哪些下一篇:

3 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18