这篇文章主要为大家展示了“怎么用Java结合SAX 2.0 解析XML文档”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么用Java结合SAX 2.0 解析XML文档”这篇文章吧。
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 -->");
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 -->");
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 -->");
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号长沙市各大影院落放映列表 -->
<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文档”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!