SAX is an abbreviation and means "Simple API for XML". A Java SAX XML parser is a stream oriented XML parser. It works by iterating over the XML and call certain methods on a "listener" object when it meets certain structural elements of the XML. For instance, it will call the listener object for the following "events":

- startDocument
- startElement
- characters
- comments
- processing instructions
- endElement
- endDocument

This list is probably not complete, but it is long enough to give you an idea of how it works. Let's move on to see how you create and use a Java SAX Parser.

SAXParserFactory factory = SAXParserFactory.newInstance();
try { InputStream xmlInput = new FileInputStream("theFile.xml");
SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new SaxHandler();
saxParser.parse(xmlInput, handler); } catch (Throwable err) {
err.printStackTrace ();
}

When you call the SAXParser.parse() method the SAX parser starts the XML processing. The xmlInput InputStream passed as parameter to the parse() method is where the XML is read from.Notice the SaxHandler instance being created, and passed as parameter to the parse() method. The SaxHandler class is a subclass of the class org.xml.sax.helpers.DefaultHandler. The DefaultHandler class comes with the JDK.

While processing the XML the SAXParser calls methods in the DefaultHandler subclass (here, the SaxHandler) instance corresponding to what the parser finds in the XML file. To react to those method calls you override the corresponding methods in the DefaultHandler subclass. Here is an example:

public class SaxHandler extends DefaultHandler {

    public void startDocument() throws SAXException {
} public void endDocument() throws SAXException {
} public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException { } public void endElement(String uri, String localName, String qName)
throws SAXException {
} public void characters(char ch[], int start, int length)
throws SAXException {
} public void ignorableWhitespace(char ch[], int start, int length)
throws SAXException {
} }

It is the responsibility of the DefaultHandler subclass to extract any necessary information from the XML via these methods. If you need to build an object graph based on an XML file, you will have to build that object graph inside the DefaultHandler subclass.

import java.io.FileInputStream;
import java.io.InputStream; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; public class TestSAXParser {
public static void test() {
SAXParserFactory factory = SAXParserFactory.newInstance();
try { InputStream xmlInput = new FileInputStream("theFile.xml");
SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new SaxHandler();
saxParser.parse(xmlInput, handler); } catch (Throwable err) {
err.printStackTrace();
}
}
public static void main(String[] args) {
TestSAXParser.test();
}
} class SaxHandler extends DefaultHandler { public void startDocument() throws SAXException {
} public void endDocument() throws SAXException {
} public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println(uri + ":" + localName + ":" + qName);
} public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println(uri + ":" + localName + ":" + qName);
} public void characters(char ch[], int start, int length)
throws SAXException {
} public void ignorableWhitespace(char ch[], int start, int length)
throws SAXException {
} }

Java SAX Parser的更多相关文章

  1. Java SAX handle xml

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

  2. Java SAX DefaultHandler

    The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0. ...

  3. Java Sax解析

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

  4. 菜鸟调错(一)——Maven项目部署到Jboss出现:Failed to create a new SAX parser

    今天调试的时候遇到一个错误,往Jboss的deploy目录扔war包的时候,报了一个“Failed to create a new SAX parser”的错误.在网上找了找解决方案,一般都说将项目中 ...

  5. jsoup: Java HTML Parser

    jsoup  Java HTML Parser jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于j ...

  6. Java sax、dom、pull解析xml

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

  7. java & xml parser

    参考: JDK8 API: http://docs.oracle.com/javase/8/docs/api/ DOM: http://www.w3.org/TR/2004/REC-DOM-Level ...

  8. jsoup: Java HTML Parser (类似jquery)

    jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extr ...

  9. Java SAX Schema Validation

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

随机推荐

  1. 【转】深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  2. Servlet课程0424(一) 通过实现Servlet接口来开发Servlet

    //这是我的第一个Servlet,使用实现Servlet接口的方式来开发 package com.tsinghua; import javax.servlet.*; import java.io.*; ...

  3. vmware tools安装程序无法继续,Microsoft Runtime DLL安装程序未能完成安装。的解决方法

    vmware tools安装程序无法继续,Microsoft Runtime DLL安装程序未能完成安装.的解决方法_华英雄_新浪博客 http://blog.sina.com.cn/s/blog_5 ...

  4. GNU :6.47 Function Names as Strings

    链接:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#Function-Names GCC provides three magic var ...

  5. Android invalidate()自动清屏,屏幕刷新

    invalidate()是用来刷新View的,必须是在UI线程中进行工作.比如在修改某个view的显示时,调用invalidate()才能看到重新绘制的界面.invalidate()的调用是把之前的旧 ...

  6. FastScroll(3)分组的listview 打开fastscroll的分组提示功能

    1,让ListView显示分组(用两个layout) 2,让ListView实现sectionIndexer接口 3,代码如下: import java.util.ArrayList; import ...

  7. 在C++中调用DLL中的函数

    如何在C++中调用DLL中的函数 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下 ...

  8. Android开发之使用AlertDialog创建对话框,单选框和多选框

    对话框: 对话框的icon,title,message等都可以不设置. 单选框和多选框与对话框勾选步骤基本上一致. 对话框的构建步骤: 1.使用AlertDialog类的内部类Builder类new ...

  9. hdu4745Two Rabbits(dp)

    链接 哎..比赛中一下想到了公共子序 之后思维就被局限了 一直在这附近徘徊 想着怎么优化 怎么预处理.. 观看了众多神牛的代码 ..以前觉得自己能写出个记忆化的最长回文长度 还挺高兴的...现在觉得好 ...

  10. poj 2993 Emag eht htiw Em Pleh(模拟)

    题目:http://poj.org/problem?id=2993 题意:和2996反着 #include <iostream> #include<cstdio> #inclu ...