SAX解析XML-例子
2024-09-26 00:15:26
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"))));
}
}
最新文章
- php导出excel封装类
- MongoDB-MMS使用总结
- Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型
- listagg( ) within group ( order by ) 与 wm_concat
- java WeakReference
- JS 函数参数
- (转)Should I use char** argv or char* argv[]
- javascript 关闭页面提示
- bridged(桥接模式)、NAT(网络地址转换模式)和host-only(主机模式)-VMware下三种网络配置方式
- Python爬虫获取异步加载站点pexels并下载图片(Python爬虫实战3)
- .NET ThreadPool算法
- VMware虚拟机安装CentOS7【转】-添加部分注释(自己看着方便)
- ubuntu 有些软件中不能输入中文
- POJ 1064 Cable master (二分法+精度控制)
- sql server 查询某个表被哪些存储过程调用
- ENVIRONMENT
- MySQL 组提交(group commit)
- 改变html中的内容
- 【51NOD】1006 最长公共子序列Lcs(动态规划)
- QT QPushButton
热门文章
- python 面向对象五 获取对象信息 type isinstance getattr setattr hasattr
- net 视图的调用和开发
- 目标Python2+3
- nginx_uWSGI_django_virtualenv_supervisor发布web服务器
- Monkey Banana Problem LightOJ - 1004
- 逆序数 UVALive 6508 Permutation Graphs
- DP UVALive 6506 Padovan Sequence
- Android IJKPlayer缓冲区设置以及播放一段时间出错解决方案
- idea 2018 解决 双击shift 弹出 search everywhere 搜索框的方法
- iOS Programming View and View Hierarchy 视图和视图等级