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"))));
}
}

最新文章

  1. php导出excel封装类
  2. MongoDB-MMS使用总结
  3. Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型
  4. listagg( ) within group ( order by ) 与 wm_concat
  5. java WeakReference
  6. JS 函数参数
  7. (转)Should I use char** argv or char* argv[]
  8. javascript 关闭页面提示
  9. bridged(桥接模式)、NAT(网络地址转换模式)和host-only(主机模式)-VMware下三种网络配置方式
  10. Python爬虫获取异步加载站点pexels并下载图片(Python爬虫实战3)
  11. .NET ThreadPool算法
  12. VMware虚拟机安装CentOS7【转】-添加部分注释(自己看着方便)
  13. ubuntu 有些软件中不能输入中文
  14. POJ 1064 Cable master (二分法+精度控制)
  15. sql server 查询某个表被哪些存储过程调用
  16. ENVIRONMENT
  17. MySQL 组提交(group commit)
  18. 改变html中的内容
  19. 【51NOD】1006 最长公共子序列Lcs(动态规划)
  20. QT QPushButton

热门文章

  1. python 面向对象五 获取对象信息 type isinstance getattr setattr hasattr
  2. net 视图的调用和开发
  3. 目标Python2+3
  4. nginx_uWSGI_django_virtualenv_supervisor发布web服务器
  5. Monkey Banana Problem LightOJ - 1004
  6. 逆序数 UVALive 6508 Permutation Graphs
  7. DP UVALive 6506 Padovan Sequence
  8. Android IJKPlayer缓冲区设置以及播放一段时间出错解决方案
  9. idea 2018 解决 双击shift 弹出 search everywhere 搜索框的方法
  10. iOS Programming View and View Hierarchy 视图和视图等级