Java SAX handle xml
https://www.journaldev.com/1198/java-sax-parser-example
Java SAX Parser Example
SAX Parser in java provides API to parse XML documents. SAX parser is different from DOM parser because it doesn’t load complete XML into memory and read xml document sequentially.
SAX Parser
javax.xml.parsers.SAXParser
provides method to parse XML document using event handlers. This class implements XMLReader
interface and provides overloaded versions of parse()
methods to read XML document from File, InputStream, SAX InputSource and String URI.
The actual parsing is done by the Handler class. We need to create our own handler class to parse the XML document. We need to implement org.xml.sax.ContentHandler
interface to create our own handler classes. This interface contains callback methods that receive notification when any event occurs. For example StartDocument, EndDocument, StartElement, EndElement, CharacterData etc.
org.xml.sax.helpers.DefaultHandler
provides default implementation of ContentHandler interface and we can extend this class to create our own handler. It’s advisable to extend this class because we might need only few of the methods to implement. Extending this class will keep our code cleaner and maintainable.
SAX parser Example
Let’s jump to the SAX parser example program now, I will explain different features in detail later on.
employees.xml
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee id="1">
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
</Employee>
<Employee id="2">
<age>35</age>
<name>Lisa</name>
<gender>Female</gender>
<role>CEO</role>
</Employee>
<Employee id="3">
<age>40</age>
<name>Tom</name>
<gender>Male</gender>
<role>Manager</role>
</Employee>
<Employee id="4">
<age>25</age>
<name>Meghna</name>
<gender>Female</gender>
<role>Manager</role>
</Employee>
</Employees>
So we have a XML file stored somewhere in file system and by looking at it, we can conclude that it contains list of Employee. Every Employee has id
attribute and fields age
, name
, gender
and role
.
We will use SAX parser to parse this XML and create list of Employee object.
Here is the Employee object representing Employee element from XML.
package com.journaldev.xml; public class Employee {
private int id;
private String name;
private String gender;
private int age;
private String role; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
} @Override
public String toString() {
return "Employee:: ID="+this.id+" Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender +
" Role=" + this.role;
} }
Let’s create our own SAX Parser Handler class extending DefaultHandler class.
package com.journaldev.xml.sax; import java.util.ArrayList;
import java.util.List; import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; import com.journaldev.xml.Employee; public class MyHandler extends DefaultHandler { //List to hold Employees object
private List<Employee> empList = null;
private Employee emp = null; //getter method for employee list
public List<Employee> getEmpList() {
return empList;
} boolean bAge = false;
boolean bName = false;
boolean bGender = false;
boolean bRole = false; @Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException { if (qName.equalsIgnoreCase("Employee")) {
//create a new Employee and put it in Map
String id = attributes.getValue("id");
//initialize Employee object and set id attribute
emp = new Employee();
emp.setId(Integer.parseInt(id));
//initialize list
if (empList == null)
empList = new ArrayList<>();
} else if (qName.equalsIgnoreCase("name")) {
//set boolean values for fields, will be used in setting Employee variables
bName = true;
} else if (qName.equalsIgnoreCase("age")) {
bAge = true;
} else if (qName.equalsIgnoreCase("gender")) {
bGender = true;
} else if (qName.equalsIgnoreCase("role")) {
bRole = true;
}
} @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("Employee")) {
//add Employee object to list
empList.add(emp);
}
} @Override
public void characters(char ch[], int start, int length) throws SAXException { if (bAge) {
//age element, set Employee age
emp.setAge(Integer.parseInt(new String(ch, start, length)));
bAge = false;
} else if (bName) {
emp.setName(new String(ch, start, length));
bName = false;
} else if (bRole) {
emp.setRole(new String(ch, start, length));
bRole = false;
} else if (bGender) {
emp.setGender(newString(ch, start, length));
bGender =false;}}}
MyHandler contains the list of Employee
object as field with a getter method only. The Employee
objects are getting added in the event handler methods. Also we have an Employee field that will be used to create Employee object and once all the fields are set, add it to the employee list.
SAX parser methods to override
The important methods to override are startElement()
, endElement()
and characters()
.
SAXParser
starts parsing the document, when any start element is found, startElement()
method is called. We are overriding this method to set boolean variables that will be used to identify the element.
We are also using this method to create new Employee object every time Employee start element is found. Check how id attribute is read here to set the Employee Object id
field.
characters()
method is called when character data is found by SAXParser inside an element. We are using boolean fields to set the value to correct field in Employee object.
The endElement()
is the place where we add Employee object to the list whenever we found Employee end element tag.
Below is the test program that uses MyHandler
to parse above XML to list of Employee objects.
package com.journaldev.xml.sax; import java.io.File;
import java.io.IOException;
import java.util.List; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import com.journaldev.xml.Employee; public class XMLParserSAX { public static void main(String[] args) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();
saxParser.parse(new File("/Users/pankaj/employees.xml"), handler);
//Get Employees list
List<Employee> empList = handler.getEmpList();
//print employee information
for(Employee emp : empList)
System.out.println(emp);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
} }
Here is the output of the above program.
Employee:: ID=1 Name=Pankaj Age=29 Gender=Male Role=Java Developer
Employee:: ID=2 Name=Lisa Age=35 Gender=Female Role=CEO
Employee:: ID=3 Name=Tom Age=40 Gender=Male Role=Manager
Employee:: ID=4 Name=Meghna Age=25 Gender=Female Role=Manager
SAXParserFactory
provides factory methods to get the SAXParser
instance. We are passing File object to the parse method along with MyHandler instance to handle the callback events.
SAXParser are little bit confusing in start but if you are working on large XML document, it provides more efficient way to read XML than DOM Parser. That’s all for SAX Parser in Java.
Reference: SAXParser, DefaultHandler
Java SAX handle xml的更多相关文章
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- Java学习笔记--xml构造与解析之Sax的使用
汇总:xml的构造与解析 http://www.cnblogs.com/gnivor/p/4624058.html 参考资料:http://www.iteye.com/topic/763895 利用S ...
- Java sax、dom、pull解析xml
-------------------------------------SAX解析xml---------------------------------- >Sax定义 SAX是一个解析速度 ...
- 用SAX解析xml文件,java
(此文为(https://www.imooc.com/video/4482)之随笔) 1.用SAX解析xml文件大致分为三步 写了一个XML文件作为例子 (1)main方法代码如下: import j ...
- Java SE之XML<二>XML DOM与SAX解析
[文档整理系列] Java SE之XML<二>XML DOM与SAX解析 XML编程:CRUD(Create Read Update Delete) XML解析的两种常见方式: DOM(D ...
- Java用SAX解析XML
要解析的XML文件:myClass.xml <?xml version="1.0" encoding="utf-8"?> <class> ...
- Android之SAX解析XML
一.SAX解析方法介绍 SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备. SAX解析器是一种基于事件的解析器,事件驱动 ...
- Java SAX DefaultHandler
The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0. ...
- java注解生成xml和包含CDATA问题
百度java生成xml,有一大推的文章,主要的生成方式一种使用Dom4J ,还有一种使用Jdk自带注解类! 下面主要整理我注解类的使用,(可以参考这篇文章Dom4J生成xml和包含CDATA问题)和x ...
随机推荐
- Python删除列表中元素
Python中列表(list)是很常用的数据结构,删除列表中的元素有几种方法 列表的remove方法 lst = [1, 1, 3, 4] lst.remove(1) # lst->[1, 3, ...
- hdu 5384 Danganronpa(字典树)
题意: f(A,B)表示:B在A中作为子串出现的次数. 题目给出n个证据,m个子弹 Ai是证据.Bi是子弹.题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和. 解析: ...
- [转]PHP 5.3.0以上推荐使用mysqlnd驱动
我们一般的使用场景,比较少关注PHP版本的问题,而且市面上提供的PHP运行环境都还是5.2系列的. 原文:http://zhangxugg-163-com.iteye.com/blog/1894990 ...
- Linux下HTTP Server
想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...
- Github优秀开源项目
王潜升 https://github.com/code4craft/webmagic 一个爬虫框架,除了不会反爬虫外(当然可以自己加)其他都很牛逼.这个项目更新还是很快的. ansi分词 htt ...
- asp.net 导出EXCEL超高兼容(不用装Excel)
用网上的代码你会发现,下载下来后,会提示"你尝试打开的的文件的格式与文件扩展名指定的格式不一致 请验证文件没有损坏且来源可信的提示,研究了好久 后来发现可以使用人家做好的组件NOPI去实现, ...
- 从文件/文件流的头字节中得到mime信息
在写网络爬虫的时候,需要根据链接来获取文件类型,将内容正确存储.之前我都是根据链接的后缀来判断的,比如: http://img12.360buyimg.com/da/20120330/88_31_Zy ...
- 一个通用的JavaScript分页
1.JavaScript代码 Pagination=function(id) { var totalNum=0; var maxNum=10; var pageUrl=""; va ...
- 那么类 Man 可以从类 Human 派生,类 Boy 可以从类 Man 派生
若在逻辑上 B 是 A 的“一种”(a kind of ),则允许 B 继承 A 的功 能和属性. 例如男人(Man)是人(Human)的一种,男孩(Boy)是男人的一种. 那么类 Man 可以从类 ...
- php -- instanceof、class_exists、insterface_exists、method_exists、get_class、get_parent_class
class_exists:类是否存在 在创建对象之前判断类是否存在,如果不存在就应该先加载类,再创建对象,容错. interface_exists:接口是否存在 method_exists:方法是否存 ...