SAX解析XML-例子
1.要解析的xml
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="001">
<name>cici</name>
<department>finace</department>
<supervisor>lily</supervisor>
</employee>
<employee id="002">
<name>alex</name>
<department>develope</department>
<supervisor>lily</supervisor>
</employee>
</employees>
2.继承DefaultHandler的子类EmployeeHandler.java,重写方法
package sax; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory; public class SaxXMLTest {
public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException{
readXMLBySaxParser();
readXMLByXMLReader();
} private static void readXMLBySaxParser() throws ParserConfigurationException,
SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser(); EmployeeHandler handler = new EmployeeHandler("employee");
parser.parse("src\\sax\\employees.xml", handler);
List<Map<String, String>> employees = handler.getEmployees();
System.out.println(employees.toString());
} private static void readXMLByXMLReader() throws SAXException,
SAXNotRecognizedException, SAXNotSupportedException, FileNotFoundException, IOException {
XMLReader reader = XMLReaderFactory.createXMLReader();
//打开解析器验证的功能
reader.setFeature("http://xml.org/sax/features/validation",true);
//开启明明空间特性
reader.setFeature("http://xml.org/sax/features/namespaces",true);
EmployeeHandler handler = new EmployeeHandler("employee");
reader.setContentHandler(handler);
reader.parse(new InputSource(new BufferedInputStream(new FileInputStream("src\\sax\\employees.xml"))));
}
}
3.测试类 SaxXMLTest.java,用SAXParser和XMLReader两种方式解析
package sax; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory; public class SaxXMLTest {
public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException{
readXMLByHandler();
readXMLByXMLReader();
} private static void readXMLByHandler() throws ParserConfigurationException,
SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser(); EmployeeHandler handler = new EmployeeHandler("employee");
parser.parse("src\\sax\\employees.xml", handler);
List<Map<String, String>> employees = handler.getEmployees();
System.out.println(employees.toString());
} private static void readXMLByXMLReader() throws SAXException,
SAXNotRecognizedException, SAXNotSupportedException, FileNotFoundException, IOException {
XMLReader reader = XMLReaderFactory.createXMLReader();
//打开解析器验证的功能
reader.setFeature("http://xml.org/sax/features/validation",true);
//开启明明空间特性
reader.setFeature("http://xml.org/sax/features/namespaces",true);
EmployeeHandler handler = new EmployeeHandler("employee");
reader.setContentHandler(handler);
reader.parse(new InputSource(new BufferedInputStream(new FileInputStream("src\\sax\\employees.xml"))));
}
}
SAX解析XML-例子的更多相关文章
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- DOM&SAX解析XML
在上一篇随笔中分析了xml以及它的两种验证方式.我们有了xml,但是里面的内容要怎么才能得到呢?如果得不到的话,那么还是没用的,解析xml的方式主要有DOM跟SAX,其中DOM是W3C官方的解析方式, ...
- 用SAX解析xml文件,java
(此文为(https://www.imooc.com/video/4482)之随笔) 1.用SAX解析xml文件大致分为三步 写了一个XML文件作为例子 (1)main方法代码如下: import j ...
- Android之SAX解析XML
一.SAX解析方法介绍 SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备. SAX解析器是一种基于事件的解析器,事件驱动 ...
- Android 使用pull,sax解析xml
pull解析xml文件 1.获得XmlpullParser类的引用 这里有两种方法 //解析器工厂 XmlPullParserFactory factory=XmlPullParserFactory. ...
- cocos2d-x 3.0 使用Sax解析xml文件(中国显示器问题解决)
今天是个好日子.我以为事情可以变得,明天是个好日子.打开门儿春风... 恩,听着歌写文档生活就是这么享受. 今天曾经的邻居大神突然在qq上赞了我一下,这让我异常激动啊.. 这还要从前前前几天说起,那会 ...
- SAX解析xml浅析
SAX解析XML文件采用事件驱动的方式进行,也就是说,SAX是逐行扫描文件,遇到符合条件的设定条件后就会触发特定的事件,回调你写好的事件处理程序.使用SAX的优势在于其解析速度较快,占用内存较少(相对 ...
- JavaWeb学习日记----SAX解析XML
1.SAX解析XML文档的方式: 与DOM方式解析不同,DOM方式解析是根据XML的层级结构在内存中分配一个树形结构,把xml的标签,属性和文本都封装成对象.优点是可以很方便实现增删改操作.缺点是,如 ...
- Python:使用基于事件驱动的SAX解析XML
SAX的特点: 是基于事件的 API 在一个比 DOM 低的级别上操作 为您提供比 DOM 更多的控制 几乎总是比 DOM 更有效率 但不幸的是,需要比 DOM 更多的工作 基于对象和基于事件的接口 ...
- Android SAX解析XML
本篇讲解一下SAX解析XML这种方式,首先来看一下它的基本介绍: SAX是一种以事件驱动的XML API,由它定义的事件流可以指定从解析器传到专门的处理程序的代码的XML结构,简单的讲,它是种解析速度 ...
随机推荐
- bzoj 3470: Freda’s Walk【拓扑排序+期望dp】
dfs会T,只好正反两遍拓扑了-- #include<iostream> #include<cstdio> #include<queue> #include< ...
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- js 编码详解
今天在整理 js编码解码方法时,在网上搜资料,发现一篇文章讲的不错,讲解的非常简单明了,于是乎就想转载过来,却发现无法转载到博客园,最后只能卑鄙的摘抄过来.js编码解码就是将一些对URL和数据库敏感的 ...
- C# 读写text 详细讲解
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...
- java基本数据类型所占字节数
JAVA基本数据类型所占字节数是多少?(32位系统) byte 1字节 short 2字节 int 4字节 ...
- [POI2009]SLO
Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次交换的代价之和.请问将(ai)变为(b ...
- CodeForces 923C Perfect Security
C. Perfect Security time limit per test3.5 seconds memory limit per test512 megabytes inputstandard ...
- 洛谷 P1288 取数游戏II
奇奇怪怪的游戏,不多写了 #include<cstdio> ]; int main() { int i; scanf("%d",&n); ;i<=n;i+ ...
- Android偏好设置(3)启动偏好设置后显示的界面PreferenceActivity和PreferenceFragment
Creating a Preference Activity To display your settings in an activity, extend the PreferenceActivit ...
- bootstrap框架栅格系统使用
使用的前端框架 bootstrap框架 Bootstrap是一个响应式的框架 我们在使用的时候主要使用的是它的网格系统, 1.bootstrap布局 布局容器:.container(用于固定宽度并支 ...