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 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 {
}
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 ...
随机推荐
- Linux---Ls命令 初级实现
By xxx0624Done: ls ls -a ls -l ls /tmp ls -R ls -t FileName color FileName o ...
- Qt官网变更【2012】
Qt最近被Digia完全收购,诺基亚这两年的不理不睬,没有魄力,不仅断送了他的手机霸主地位,也耽误了Qt这两年的快速发展. 希望Digia能让Qt真正实现 run everywhere. 最近Qt的官 ...
- ArcGIS学习记录—Arcgis中点、线、面的相互转换方法
本文使用的工具在Arctoolbox.Data Management Tools.Features (一)面--面转线.面转点 面转线 Polygon To Line .Feature To Lin ...
- Apache Tomcat下载、安装、配置图文教程
本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/19/Tomcat安装配置/ (整理截图.安装过程更加 ...
- 转:C语言宏定义时#(井号)和##(双井号)的用法
转自:http://www.cnblogs.com/welkinwalker/archive/2012/03/30/2424844.html#2678295 #在英语里面叫做 pound 在C语言的宏 ...
- RabbitMQ安装和配置
RabbitMQ: MQ:message queue.MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来 ...
- 关于Firefox浏览器如何支持ActiveX控件,一个小的Hellow World
今天尝试开发一个Firefox的插件.虽然比较简单,网上也有很多教程,但是感觉一些教程写的比较麻烦,在初步的开发过程中并没有用到那些东西,于是自己把开发过程记录下来.我是根据Mozilla官方教程开发 ...
- 在C++中调用DLL中的函数
如何在C++中调用DLL中的函数 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下 ...
- notepad++ 编辑器链接地址可点击
很久没用notepad++编辑器,最近因为 sublime 的编码问题,因此用了下 notepad++ .结果发现里面的链接都可以点击,一点都不要编辑,如下图: 那如何把这个功能去掉呢? 我们选择 菜 ...
- Js动态传递不定数目的参数
回调程序中,经常有这样的需求:用户传递一个回调方法,该方法可以有不定的参数. 如果参数数目固定则很容易实现,看代码: //回调函数1 function callback1(a,b,c) { alert ...