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. loadrunner java协议脚本要点

    常见问题 1. Error: Thread Context: Call to service of the driver failed, reason - thread context wasn't ...

  2. 双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计

    前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构. 架构简介 此架构主要是由ke ...

  3. Adobe Acrobat XI Pro安装破解

    注册机使用说明: Install Instructions: (Read carefully!) 安装说明(仔细阅读!) 1. Disable your Network card or pull th ...

  4. java遍历树(深度遍历和广度遍历

    java遍历树如现有以下一颗树:A     B          B1               B11          B2               B22     C          C ...

  5. jQgrid问题总结

    最近一段时间一直在使用jqgrid这个免费的插件,网上的资料也比较多.比较全,但是这里还是整理几个自己在开发过程中遇到的小问题. 1.自动换行 一行数据过多需要自动根据内容换行时,如果遇到在表格中的汉 ...

  6. Innodb 锁系列2 事务锁

    上一篇介绍了Innodb的同步机制锁:Innodb锁系列1 这一篇介绍一下Innodb的事务锁,只所以称为事务锁,是因为Innodb为实现事务的ACID特性,而添加的表锁或者行级锁. 这一部分分两篇来 ...

  7. poj3671

    首先容易想到的是LIS,但是n<=30000,所以肯定要优化: 壮哉单调队列又登场了: 然后再找一个最长不上升序列并求两者最大值即可,复杂度O(n logn); 应该说这是解题通法了,但再回头看 ...

  8. Css3 Media Queries移动页面的样式和图片的适配问题(转)

    CSS3 Media Queries 摘自:http://www.w3cplus.com/content/css3-media-queries Media Queries直译过来就是“媒体查询”,在我 ...

  9. ExtJs批量更新

    昨天这个批量更新花了我不少时间,特记下来,省得以后忘记. 批量更新的逻辑是这样的. 获取Store中需要更新的行,把行放入数组,然后再将数组转化为Json字符串,Json字符串传后后台后,解析为实体列 ...

  10. 【 D3.js 高级系列 — 2.0 】 捆图

    捆图(Bundle)是 D3 中比较奇特的一个布局,只有两个函数,而且需要与其它布局配合使用.本文讲述捆图的制作方法. 有关捆图的例子极少,很容易找到的是:http://bl.ocks.org/mbo ...