Java SAX DefaultHandler
The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0.
As shown briefly in the first text on SAX in this tutorial, you create a subclass of DefaultHandler and override certain inherited methods in this subclass. In this text I will show a very simple example of a DefaultHandler subclass, which just prints out detail about the XML file. Here is the code:
public class SaxHandler extends DefaultHandler {
public void startDocument() throws SAXException {
System.out.println("start document : ");
}
public void endDocument() throws SAXException {
System.out.println("end document : ");
}
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
System.out.println("start element : " + qName);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("end element : " + qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("start characters : " +
new String(ch, start, length));
}
}
When you run this code, with this file as input:
<root>
<child>
<grandchild>text 1</grandchild>
</child>
<child>
<grandchild>text 2</grandchild>
</child>
</root>
... you get the following output printed to the System.out:
start document :
start element : root
characters : start element : child
characters : start element : grandchild
characters : text 1
end element : grandchild
characters : end element : child
characters : start element : child
characters : start element : grandchild
characters : text 2
end element : grandchild
characters : end element : child
characters : end element : root
end document :
This is the sequence in which the SAXParser calls the corresponding methods in the SaxHandler instance, when processing the XML file shown above.
You may have noticed that sometimes the characters() method prints out a line break. This is because thecharacters() method is called by the SAXParser with the whitespace characters that are located between the end of the parent element begin tag, and the child begin tag. In other words, the white space characters marked here using dots (...):
<root>...
....<child>
</child>
There are also sometimes whitespace characters located after the end of an element end tag, and until the beginning of the next sibling tag, or the beginning of the end tag of the parent element.
Processing Instructions
The DefaultHandler class also has a method for when XML processing instructions are found in the XML file. Here is how that method looks:
public void processingInstruction(String target, String data)
throws SAXException {
}
You don't very often use processing instructions, so I won't get into more detail about it here. Now that you know it is here, you can play with it yourself.
Exceptions
The DefaultHandler class has three methods you can override to handle exceptions encountered during the XML parsing. Here they are:
public void warning(SAXParseException e) throws SAXException {
}
public void error(SAXParseException e) throws SAXException {
}
public void fatalError(SAXParseException e) throws SAXException {
}
Java SAX DefaultHandler
The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0.
As shown briefly in the first text on SAX in this tutorial, you create a subclass of DefaultHandler and override certain inherited methods in this subclass. In this text I will show a very simple example of a DefaultHandlersubclass, which just prints out detail about the XML file. Here is the code:
public class SaxHandler extends DefaultHandler {
public void startDocument() throws SAXException {
System.out.println("start document : ");
}
public void endDocument() throws SAXException {
System.out.println("end document : ");
}
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
System.out.println("start element : " + qName);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("end element : " + qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("start characters : " +
new String(ch, start, length));
}
}
When you run this code, with this file as input:
<root>
<child>
<grandchild>text 1</grandchild>
</child>
<child>
<grandchild>text 2</grandchild>
</child>
</root>
... you get the following output printed to the System.out:
start document :
start element : root
characters : start element : child
characters : start element : grandchild
characters : text 1
end element : grandchild
characters : end element : child
characters : start element : child
characters : start element : grandchild
characters : text 2
end element : grandchild
characters : end element : child
characters : end element : root
end document :
This is the sequence in which the SAXParser calls the corresponding methods in the SaxHandler instance, when processing the XML file shown above.
You may have noticed that sometimes the characters() method prints out a line break. This is because thecharacters() method is called by the SAXParser with the whitespace characters that are located between the end of the parent element begin tag, and the child begin tag. In other words, the white space characters marked here using dots (...):
<root>...
....<child>
</child>
There are also sometimes whitespace characters located after the end of an element end tag, and until the beginning of the next sibling tag, or the beginning of the end tag of the parent element.
Processing Instructions
The DefaultHandler class also has a method for when XML processing instructions are found in the XML file. Here is how that method looks:
public void processingInstruction(String target, String data)
throws SAXException {
}
You don't very often use processing instructions, so I won't get into more detail about it here. Now that you know it is here, you can play with it yourself.
Exceptions
The DefaultHandler class has three methods you can override to handle exceptions encountered during the XML parsing. Here they are:
public void warning(SAXParseException e) throws SAXException {
}
public void error(SAXParseException e) throws SAXException {
}
public void fatalError(SAXParseException e) throws SAXException {
}
Let's say that the parser encounters an illegal XML entity (like ¬Legal;). The SAXParser will then call thefatalError() method, before breaking the parsing.
If a less dangerous error occurs, the SAXParser may just call the error() or warning() method. That way you can collect all the errors in a list, and return them all at once, instead of one by one, as they are met.
Additional Methods
The DefaultHandler has more methods you can override. Check out the JavaDoc for more details on those methods.
Java SAX DefaultHandler的更多相关文章
- Java Sax解析
一. Java Sax解析是按照xml文件的顺序一步一步的来解析,在解析xml文件之前,我们要先了解xml文件的节点的种类,一种是ElementNode,一种是TextNode.如下面的这段boo ...
- Java SAX Parser
SAX is an abbreviation and means "Simple API for XML". A Java SAX XML parser is a stream o ...
- Java SAX handle xml
https://www.journaldev.com/1198/java-sax-parser-example Java SAX Parser Example SAX Parser in java ...
- SAX - DefaultHandler
org.xml.sax.helpers.DefaultHandler 实现了 org.xml.sax.EntityResolver.org.xml.sax.DTDHandler.org.xml.sax ...
- Java sax、dom、pull解析xml
-------------------------------------SAX解析xml---------------------------------- >Sax定义 SAX是一个解析速度 ...
- Java SAX解析器
SAX(针对XML的简单API)是基于事件为XML文档的解析器.不像DOM解析器,SAX解析器创建没有解析树. SAX是一个流接口用于XML的,这意味着使用SAX应用接收事件通知有关XML文档被处理的 ...
- Java SAX Schema Validation
It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it look ...
- java解析XML之DOM解析和SAX解析(包含CDATA的问题)
Dom解析功能强大,可增删改查,操作时会将XML文档读到内存,因此适用于小文档: SAX解析是从头到尾逐行逐个元素解析,修改较为不便,但适用于只读的大文档:SAX采用事件驱动的方式解析XML.如同在电 ...
- JAVA解析XML之SAX方式
JAVA解析XML之SAX方式 SAX解析xml步骤 通过SAXParseFactory的静态newInstance()方法获取SAXParserFactory实例factory 通过SAXParse ...
随机推荐
- orm框架与缓存的关系
1.mybatis规定,一级缓存没必要bean类实现序列化,但二级缓存bean类必须实现序列化. 因为二级缓存是基于namespace的也就是基于接口的,二级缓存可以设置存储源,可以是redis或者m ...
- JavaWeb学习总结(四十九)——简单模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- inflate方法与findViewById的区别
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来找 re ...
- 如何将sql server数据库转化成sqlite数据库
今天在将sql server转化为sqlite的数据库的时候,遇到不少的问题,在网上搜了很长时间,都没有找到合适的软件将sql server转化成sqlite,其中用到了SqliteDev软件,在转化 ...
- 【Lucene3.6.2入门系列】第04节_中文分词器
package com.jadyer.lucene; import java.io.IOException; import java.io.StringReader; import org.apach ...
- linux地址映射1、2、3(☆☆☆)
欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 一.线性映射与非线性映射 ...
- cookie分析
浏览器cookie分析 简单点说就是数据存储,通过JavaScript将要保存的数据保存在客户端浏览器,下次打开网页时调用保存的cookie.浏览器中cookie保存的形式:user=aa; pwd= ...
- 【HDOJ】4374 One hundred layer
线性DP,使用单调队列优化. /* 4374 */ #include <iostream> #include <sstream> #include <string> ...
- python中的commands模块
commands模块用于调用shell命令 有3中方法: commands.getstatus() 返回执行状态 commands.getoutput() 返回执行结果 commands.ge ...
- git恢复被修改的文件
恢复到最后一次提交的改动: git checkout -- + 需要恢复的文件名 但是,需要注意的是,如果该文件已经 add 到暂存队列中,上面的命令就不灵光喽 需要先让这个文件取消暂存: git r ...