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. PreparedStatement的用法

    转载:http://www.cnblogs.com/raymond19840709/archive/2008/05/12/1192948.html jdbc(java database connect ...

  2. [itint5]交替字符串

    http://www.itint5.com/oj/#17 DP.注意曾经把赋值写成了==,结果出错半天. bool isInterleaving(string &str1, string &a ...

  3. Android:布局合集

    本文归纳Android布局中所用到的知识点,网络上的教程说得太细化,而对于前端来说,下面的归纳最适合不过了. Android五大布局: LinearLayout 线性布局 Relativelayout ...

  4. CodeForces152C——Pocket Book(排列组合问题)

    Pocket Book DescriptionOne day little Vasya found mom's pocket book. The book had n names of her fri ...

  5. C# 调用WebService的方法

    很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...

  6. 7个鲜为人知却超实用的PHP函数

    PHP有许多内置函数,其中大多数函数都被程序员广泛使用.但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数. 没用过的程序员不妨过来看看. 1.highlight_string ...

  7. Linux C/C++ 编程练手 --- 大数相加和大数相乘

    最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...

  8. notepad++ 编辑器链接地址可点击

    很久没用notepad++编辑器,最近因为 sublime 的编码问题,因此用了下 notepad++ .结果发现里面的链接都可以点击,一点都不要编辑,如下图: 那如何把这个功能去掉呢? 我们选择 菜 ...

  9. java RuntimeException

    总结了一下JAVA中常见的几种RuntimeException,大约有如下几种: NullPointerException - 空指针引用异常 ClassCastException - 类型强制转换异 ...

  10. 【Java】【编译】javac编译源代码时,若源文件使用了别的java源代码的函数,javac会自动关联。

    * 算法第四版自己的alg4.jar似乎有些过时. * 可以引用别的源码文件里的函数