sax方式解析xml 文档跟dom最大的区别是,sax是基于读取到xml文档不同节点的产生不同的事件,然后回调Saxparse处理器里面的不同方法对节点进行不同处理。所以sax是基于事件触发机制的解析方式。
dom解析呢是根据xml文档建立dom节点树,然后对树里面的节点遍历从而对xml文档解析的
- package 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;
-
- //定义一个自定义sax处理器,用于读取到element节点时回调其中的方法。
- ublic class Saxparse extends DefaultHandler {
-
- // 定义一个存放元素的list
- private List<Book> data;
- // 用于缓存当前标签名称
- private String elementName;
-
- public List<Book> getData() {
- return data;
-
- }
-
- /** * 当遇到节点前半部分时候,如<price>触发这个方法 * * /** * xml如果没有约束 uri # null localname #null qName # 元素名称 * xml 约束 uri # * 命名空间值 targatNamespace="xxxxx" localname # 元素名称 * <table> * qName # 前缀:名称 <my:table> * */
- @Override
- public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
- // TODO Auto-generated method stub
-
- if ("book".equals(qName)) {
- // 说明解析到了一本新书
- Book book = new Book();
- data.add(book);// 添加到list中
- book.setId(attributes.getValue("id")); // 获取book的属性
- // 缓存当前标签名称
-
- }
- this.elementName = qName;
- }
-
- /** * 遇到如下字符串触发这个函数 <?xml version="1.0" encoding="UTF-8"?> * * @throws SAXException */
- @Override
- public void startDocument() throws SAXException {
- // TODO Auto-generated method stub
- super.startDocument();
- System.out.println("解析文档开始");
- data = new ArrayList<>();
- }
-
- /** * 遇到一个节点的后半部,如</price> * * @param uri * @param localName * @param qName * @throws SAXException */
- @Override
- public void endElement(String uri,String qName) throws SAXException {
- // TODO Auto-generated method stub
- super.endElement(uri,localName,qName);
- System.out.println("元素结束" + "\tqName=" + qName + "\t localName=" + null);
- }
-
- @Override
- public void endDocument() throws SAXException {
- // TODO Auto-generated method stub
- super.endDocument();
- System.out.println("文档结束");
- }
-
- /** * 取每个节点下的文本串,如下面的100 <price> 100 </price> * * @param ch * @param start * @param length * @throws org.xml.sax.SAXException */
- @Override
- public void characters(char[] ch,int start,int length) throws org.xml.sax.SAXException {
- // TODO Auto-generated method stub
- super.characters(ch,start,length);
- String value = new String(ch,length);
- if ("title".equals(elementName)) {
- // value 表示title的数据
- data.get(data.size() - 1).setTitle(value);
-
- }
- if ("price".equals(elementName)) {
- // value 表示price的数据
- data.get(data.size() - 1).setPrice(value);
-
- }
- if ("author".equals(elementName)) {
- data.get(data.size() - 1).setAuthor(value);
-
- }
- this.elementName = null;
- }
-
- public Saxparse() {
- // TODO Auto-generated constructor stub
- }
-
- }
定义一个javabean,和xml一个book节点是一致的
- package sax;
-
- public class Book {
- public Book() {
- // TODO Auto-generated constructor stub
- }
-
- @Override
- public String toString() {
- return "Book [id=" + id + ",title=" + title + ",price=" + price + ",author=" + author + "]";
- }
-
- private String id;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String getPrice() {
- return price;
- }
-
- public void setPrice(String price) {
- this.price = price;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- private String title;
- private String price;
- private String author;
-
- }
用于解析的xml文档
- <?xml version="1.0" encoding="UTF-8"?>
- <books>
- <book id="b001">
- <title>android</title>
- <price>28</price>
- <author>vincent</author>
- </book>
- <book id="b002">
- <title>Vincent</title>
- <price>Vincent</price>
- <author>Vincent</author>
- </book>
- <book id="b0003">
- <title>Flex开发</title>
- <price>98</price>
- <author>胡玉勉</author>
-
- </book>
-
- <book id="b0003">
- <title>Flex开发</title>
- <price>98</price>
- <author>胡玉勉</author>
- </book>
- <book id="b0003">
-
-
-
- <title>Flex开发</title>
- <price>98</price>
- <author>胡玉勉</author>
-
- </book>
- <book id="b0003">
- <title>Flex开发</title>
- <price>98</price>
- <author>胡玉勉</author>
-
- </book>
-
- </books>
测试例程
- package sax.testrun;
-
- 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 sax.Book;
- import sax.Saxparse;
-
- public class RunSax {
-
- public static void main(String[] args) throws ParserConfigurationException,SAXException {
- demo1();
- }
-
- private static void demo1() throws ParserConfigurationException,SAXException {
- // 1、工厂
- SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
- // 2、解析器
- SAXParser saxParser = saxParserFactory.newSAXParser();
- Saxparse handler = new Saxparse();
- try {
- saxParser.parse(new File("src/data.xml"),handler);
- List<Book> books = handler.getData();
- for (Book book : books) {
- System.out.println(book);
-
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ;
- }
- }
输出结果
解析文档开始 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=books localName=null 文档结束 Book [id=b001,title=android,price=28,author=vincent] Book [id=b002,title=Vincent,price=Vincent,author=Vincent] Book [id=b0003,title=Flex开发,price=98,author=胡玉勉] Book [id=b0003,author=胡玉勉]