Java SAX Parser
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的更多相关文章
- Java SAX handle xml
https://www.journaldev.com/1198/java-sax-parser-example Java SAX Parser Example SAX Parser in java ...
- Java SAX DefaultHandler
The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0. ...
- Java Sax解析
一. Java Sax解析是按照xml文件的顺序一步一步的来解析,在解析xml文件之前,我们要先了解xml文件的节点的种类,一种是ElementNode,一种是TextNode.如下面的这段boo ...
- 菜鸟调错(一)——Maven项目部署到Jboss出现:Failed to create a new SAX parser
今天调试的时候遇到一个错误,往Jboss的deploy目录扔war包的时候,报了一个“Failed to create a new SAX parser”的错误.在网上找了找解决方案,一般都说将项目中 ...
- jsoup: Java HTML Parser
jsoup Java HTML Parser jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于j ...
- Java sax、dom、pull解析xml
-------------------------------------SAX解析xml---------------------------------- >Sax定义 SAX是一个解析速度 ...
- java & xml parser
参考: JDK8 API: http://docs.oracle.com/javase/8/docs/api/ DOM: http://www.w3.org/TR/2004/REC-DOM-Level ...
- jsoup: Java HTML Parser (类似jquery)
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extr ...
- Java SAX Schema Validation
It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it look ...
随机推荐
- Java 垃圾回收机制
1.delete是C++里面用于释放内存的运算符,而不是Java. 2.当发现某个对象的引用计数为0时,就将对象列入待回收列表中,并不是马上予以销毁. 3.System.gc()仅仅是一个回收请求,J ...
- 李洪强iOS开发之使用CycleScrollView实现轮播图
01 导入头文件,并且定义CycleScrollView属性 02 初始化,设置frame并且添加到collectionView上 03 调用方法并且设置轮播的图片
- [itint5]支持删除的后继查询
http://www.itint5.com/oj/#49 这一题一开始想到是用HashSet+链表来做,链表记录prev和next.这样也可以,后来看到都是连续的整数,而且交流了一下觉得可以用类似并查 ...
- C++ eof()函数相关应用技巧分享
C++编程语言中的很多功能在我们的实际应用中起着非常大的作用.比如在对文件文本的操作上,就可以用多种方式来实现.在这里我们介绍的C++ eof()函数就是其中一个比较常用的基本函数. 在使用C/C++ ...
- P117、面试题18:树的子结构
题目:输入两棵二叉树A和B,判断B是不是A的子结构.二叉树结点的定义如下:struct BinaryTreeNode{ int m_nValue; BinaryTreeNod ...
- 睡眠--TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE
http://i.cnblogs.com/EditPosts.aspx?opt=1 Two states are associated with sleeping, TASK_INTERRUPTI ...
- Linux下的动态连接库及其实现机制
Linux与Windows的动态连接库概念相似,但是实现机制不同.它引入了GOT表和PLT表的概念,综合使用了多种重定位项,实现了"浮动代码",达到了更好的共享性能.本文对这些技术 ...
- 页面滚动动态加载数据,页面下拉自动加载内容 jquery
<!DOCTYPE=html> <html> <head> < script src="js/jquery.js" type=" ...
- C语言计算程序运行时间
#include<stdio.h>#include<stdlib.h> #include "time.h" int main( void ) { ...
- vijosP1026毒药?解药?
hash. 怎么感觉叫状态压缩bfs比较合适呢? #include<cstdio> #include<algorithm> #include<cstring> us ...