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

 
By Jakob Jenkov

 Connect with me: 
Rate article:
Share article:

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 &notLegal;). 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的更多相关文章

  1. Java Sax解析

    一.   Java Sax解析是按照xml文件的顺序一步一步的来解析,在解析xml文件之前,我们要先了解xml文件的节点的种类,一种是ElementNode,一种是TextNode.如下面的这段boo ...

  2. Java SAX Parser

    SAX is an abbreviation and means "Simple API for XML". A Java SAX XML parser is a stream o ...

  3. Java SAX handle xml

    https://www.journaldev.com/1198/java-sax-parser-example Java SAX Parser Example   SAX Parser in java ...

  4. SAX - DefaultHandler

    org.xml.sax.helpers.DefaultHandler 实现了 org.xml.sax.EntityResolver.org.xml.sax.DTDHandler.org.xml.sax ...

  5. Java sax、dom、pull解析xml

    -------------------------------------SAX解析xml---------------------------------- >Sax定义 SAX是一个解析速度 ...

  6. Java SAX解析器

    SAX(针对XML的简单API)是基于事件为XML文档的解析器.不像DOM解析器,SAX解析器创建没有解析树. SAX是一个流接口用于XML的,这意味着使用SAX应用接收事件通知有关XML文档被处理的 ...

  7. Java SAX Schema Validation

    It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it look ...

  8. java解析XML之DOM解析和SAX解析(包含CDATA的问题)

    Dom解析功能强大,可增删改查,操作时会将XML文档读到内存,因此适用于小文档: SAX解析是从头到尾逐行逐个元素解析,修改较为不便,但适用于只读的大文档:SAX采用事件驱动的方式解析XML.如同在电 ...

  9. JAVA解析XML之SAX方式

    JAVA解析XML之SAX方式 SAX解析xml步骤 通过SAXParseFactory的静态newInstance()方法获取SAXParserFactory实例factory 通过SAXParse ...

随机推荐

  1. Java知识大全

    http://blog.csdn.net/zhangerqing/article/details/8245560

  2. Netcat for Windows

    April 10, 2009 Netcat is a simple networking utility which reads and writes data across network conn ...

  3. MySQL追加注释或者大量修改注释

     MySQL追加注释或者大量修改注释 2016-01-25 20:28:05 分类: MySQL MySQL 5.6.14 之前一个项目比较仓促,开发给的建表语句没有注释.现在要补全注释信息.但是My ...

  4. ActiveMQ可靠性机制

    消息的签收(Acknowledgment): 客户端成功接收一条消息的标志是这条消息被签收. 成功接收一条消息一般包括如下三个阶段: (1) 客户端接收消息  (2) 客户端处理消息   (3) 消息 ...

  5. Android2.3.7源码结构分析

    对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分.为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析.希望对刚加 ...

  6. HashMap Hashtable ArrayList HashSet

    一.散列 1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key ...

  7. 转: Linux 技巧:让进程在后台可靠运行的几种方法

    我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败.如何让命令提交后不受本地关闭终端窗口/网络断开 ...

  8. javascript 一些需要知道的东西

    这里我直接贴出代码,注释已经补全,欢迎指正: <script type="text/javascript"> /** 1,js中一切皆是对象,函数也是, 2,当定义变量 ...

  9. c语言typedef关键字的理解

    1.typedef的定义 很多人认为typedef 是定义新的数据类型,这可能与这个关键字有关.本来嘛,type 是数据类型的意思:def(ine)是定义的意思,合起来就是定义数据类型啦. 不过很遗憾 ...

  10. java中正则表达式

    在<java编程思想>中,java中的 \\ 表示“我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义.”如果想插入一个普通的反斜线,那么应该使用 \\\\. 理解: 我们使用的 ...