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 ...
随机推荐
-
asp.net ueditor 图片上传路径问题解决
最近练习做一个新闻系统,其中不能少了添加新闻和修改新闻的功能 ,而且还要添加图片.添加文字样式, 所以不得不使用富文本编辑器,在kindeditor和ueditor中,选择了目前还在持续更新的百度产品 ...
-
[转] DAG算法在hadoop中的应用
http://jiezhu2007.iteye.com/blog/2041422 大学里面数据结构里面有专门的一章图论,可惜当年没有认真学习,现在不得不再次捡 起来.真是少壮不努力,老大徒伤悲呀!什么 ...
-
Java进阶(七)正确理解Thread Local的原理与适用场景
原创文章,始自发作者个人博客,转载请务必将下面这段话置于文章开头处(保留超链接). 本文转发自技术世界,原文链接 http://www.jasongj.com/java/threadlocal/ Th ...
-
Flex 将默认日期格式转化成通用格式
flex 默认日期格式如:Wed Dec 16 00:00:00 GMT+0800 2015 想要得到的通用格式如:2015-12-16 转换方法如下: var sdate:String = &quo ...
-
解决laravel Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found 错误
这个错误的原因来自于没有安装一个依赖库: 官方文档说明如下: Modifying Columns Prerequisites Before modifying a column, be sure to ...
-
jquery获取元素节点
常用到的知识点,在此记录,以便查阅 $('.test').parent();//父节点 $('.test').parents();//全部父节点 $('.test').parents('.test1' ...
-
git删除掉已经保存的用户名密码
以保存的用户名密码删除,先找到变量存在的位置: git config -l To help track down the setting, I'd try to use: git config --l ...
-
TMS-规划图
规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 ...
-
如何使用 lsyncd 实时同步并执行 shell 命令
修改 lsyncd 的默认配置,不直接执行rsync 进行同步,而是改用自己的脚本. binary 指定我们的脚本 vim /usr/local/lsyncd/etc/lsyncd.conf sett ...
-
openstack-networking-neutron(一)---端到端和点到点的理解
本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. ====本文目的===== 理解搞清楚两个概念: 1.端到端 2.点到点 ...