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. 使用eclipse远程调试Tomcat的方法

    tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢? 1. 在eclispe中新建we ...

  2. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  3. ThinkPHP下隐藏index.php以及URL伪静态

    第一种方法: 设置url的重写模式(默认模式是1) 'URL_MODEL' => 2, // URL访问模式,可选参数0.1.2.3,代表以下四种模式: 第二种方法:  使用Apache来进行设 ...

  4. [Unity菜鸟] 材质

    1. 材质定义: 2. 把材质都改成支持透明通道 因为物体太多了,比如树跟房子材质必须用不一样的.所以办法还是你得改每个材质的Shader,都改成支持透明通道的. 在Project的搜索窗口输入t: ...

  5. Python之函数篇

    函数形参 函数取得的参数是你提供给函数的值,这样函数就可以利用这些值 做 一些事情.这些参数就像变量一样,只不过它们的值是在我们调用函数的时候定义的,而非在函数本身内赋值. 参数在函数定义的圆括号对内 ...

  6. SQL 两张结构一样的表合并查询 .

    select * from table1 union all select * from table2 union all 是所有的都显示出来: select * from table1 union ...

  7. HTML DOM与XML DOM之间,既有区别

    http://kb.cnblogs.com/page/74971/ HTML文档可以使用Core API和HTML API两者: 而XML文档只能使用Core API. 换句话说,HTML与XML重叠 ...

  8. 从今天起,正式步入cnblogs,向曾经的脚印说声对不起!

    步入这个行业也好多年了,从来没有定居过一个地方. 看过很多前辈们留下的资料,对后者门(其中还有我)留下很多珍贵的东西. 所以,我要向前辈学习,壮大自己,在学习的同时,不要忘记帮助别人. 对曾经我留下的 ...

  9. android系统平台显示驱动开发简要:LCD常用接口篇『二』

    平台信息:内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博客 ...

  10. C语言字符串函数

    strtok()     字符串分割函数strstr()     字符串查找函数 范例 #include <string.h> main() {     char * s = " ...