标签:
public void startDocument() throws SAXException {
}
public void endDocument() throws SAXException {
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
}
public void endElement(String uri, String localName, String qName) throws SAXException {
}
public void characters(char[] ch, int start, int length) throws SAXException {
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
package com.qunar.handler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserHandler extends DefaultHandler{
// 用来标示解析开始
@Override
public void startDocument() throws SAXException {
}
// 用来标示解析结束
@Override
public void endDocument() throws SAXException {
}
// 用来遍历XML文件的开始标签
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
}
// 用来遍历XML文件的结束标签
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
}
}
// 定义SAXParserHandler对象
SAXParserHandler handler = new SAXParserHandler();
saxParser.parse(path, handler);
<?xml version="1.0" encoding="utf-8"?><bookstore>
<book category="Java">
<title lang="chi">Java多线程编程核心技术</title>
<author>高洪岩</author>
<year>2015</year>
<price>69.00</price>
</book>
<book category="C++">
<title lang="en">Effective C++: 55 Specific Ways to Improve Your Programs and Designs</title>
<author>Scott Meyers</author>
<year>2006</year>
<price>58.00</price>
</book>
<book category="Web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2016</year>
<price>39.95</price>
</book>
</bookstore>
package com.qunar.handler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserHandler extends DefaultHandler{
private int bookIndex = 0;
// 用来标示解析开始
@Override
public void startDocument() throws SAXException {
System.out.println("SAX解析开始...");
}
// 用来标示解析结束
@Override
public void endDocument() throws SAXException {
System.out.println("SAX解析结束...");
}
// 用来遍历XML文件的开始标签
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// 调用DefaultHandler类的startElement方法
super.startElement(uri, localName, qName, attributes);
// 开始解析book元素节点
if(qName.equals("book")){
++ bookIndex;
System.out.println("开始解析第" + bookIndex + "本书...");
// 已知book元素节点下的属性名称,根据属性名称获取属性值
/*String value = attributes.getValue("category");
System.out.println("value->"+value);*/
// 不知道book元素节点下的属性名称以及个数
int size = attributes.getLength();
for(int i = 0;i < size;++i){
System.out.println(attributes.getQName(i) + ":" + attributes.getValue(i));
}//for
}//if
else if(!qName.equals("bookstore")){
System.out.print(qName + ":");
}//else
}
// 用来遍历XML文件的结束标签
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
// 判断一本书是否解析完
if(qName.equals("book")){
System.out.println("结束解析第" + bookIndex + "本书...");
}//if
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
String text = new String(ch, start, length);
if(!text.trim().equals("")){
System.out.println(text);
}//if
}
}
package com.qunar.xml;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import com.qunar.handler.SAXParserHandler;
/**
* SAX方式解析XML文档
* @author sjf0115
*
*/
public class SAXXMLCode {
public static void main(String[] args) {
String path = "D:\\bookstore.xml";
try {
// 通过SAXParserFactory的静态方法newInstance()方法获取SAXParserFactory实例对象factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// 通过SAXParserFactory实例的newSAXParser()方法返回SAXParser实例parser
SAXParser saxParser = factory.newSAXParser();
// 定义SAXParserHandler对象
SAXParserHandler handler = new SAXParserHandler();
// 解析XML文档
saxParser.parse(path, handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SAX解析开始...
开始解析第1本书...
category:Java
title:Java多线程编程核心技术
author:高洪岩
year:2015
price:69.00
结束解析第1本书...
开始解析第2本书...
category:C++
title:Effective C++: 55 Specific Ways to Improve Your Programs and Designs
author:Scott Meyers
year:2006
price:58.00
结束解析第2本书...
开始解析第3本书...
category:Web
title:Learning XML
author:Erik T. Ray
year:2016
price:39.95
结束解析第3本书...
SAX解析结束...
|
package com.qunar.bean;
/**
* book实体类
* @author sjf0115
*
*/
public class Book {
private String category;
private String title;
private String author;
private String year;
private String price;
private String lang;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
@Override
public String toString() {
return "category:" + category + " lang:" + lang + " title:" + title + " author:" + author + " year:" + year + " price:" + price;
}
}
package com.qunar.handler;
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.qunar.bean.Book;
public class SAXParserHandler extends DefaultHandler{
private Book book;
private int bookIndex = 0;
// 节点文本内容
private String text;
private List<Book> bookList = new ArrayList<Book>();
public List<Book> getBookList() {
return bookList;
}
// 用来标示解析开始
@Override
public void startDocument() throws SAXException {
System.out.println("SAX解析开始...");
}
// 用来标示解析结束
@Override
public void endDocument() throws SAXException {
System.out.println("SAX解析结束...");
}
// 用来遍历XML文件的开始标签
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// 调用DefaultHandler类的startElement方法
super.startElement(uri, localName, qName, attributes);
// 开始解析book元素节点
if(qName.equals("book")){
// 创建一个book对象
book = new Book();
++ bookIndex;
System.out.println("开始解析第" + bookIndex + "本书...");
int size = attributes.getLength();
for(int i = 0;i < size;++i){
String attr = attributes.getQName(i);
// 属性category
if(attr.equals("category")){
book.setCategory(attributes.getValue(i));
}//if
}//for
}//if
// 用于遍历title节点中的属性
else if(qName.equals("title")){
int size = attributes.getLength();
for(int i = 0;i < size;++i){
String attr = attributes.getQName(i);
// 属性category
if(attr.equals("lang")){
book.setLang(attributes.getValue(i));
}//if
}//for
}//else
}
// 用来遍历XML文件的结束标签
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
// 判断一本书是否解析完
if(qName.equals("book")){
bookList.add(book);
book = null;
System.out.println("结束解析第" + bookIndex + "本书...");
}//if
else if(qName.equals("title")){
book.setTitle(text);
}//else
else if(qName.equals("author")){
book.setAuthor(text);
}//else
else if(qName.equals("year")){
book.setYear(text);
}//else
else if(qName.equals("price")){
book.setPrice(text);
}//else
}
// 文本值
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
text = new String(ch, start, length);
}
}
package com.qunar.xml;
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.qunar.bean.Book;
import com.qunar.handler.SAXParserHandler;
/**
* SAX方式解析XML文档
* @author sjf0115
*
*/
public class SAXXMLCode {
public static void main(String[] args) {
String path = "D:\\bookstore.xml";
try {
// 通过SAXParserFactory的静态方法newInstance()方法获取SAXParserFactory实例对象factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// 通过SAXParserFactory实例的newSAXParser()方法返回SAXParser实例parser
SAXParser saxParser = factory.newSAXParser();
// 定义SAXParserHandler对象
SAXParserHandler handler = new SAXParserHandler();
// 解析XML文档
saxParser.parse(path, handler);
// 得到遍历结果
List<Book> bookList = handler.getBookList();
System.out.println("遍历结果:");
for (Book book : bookList) {
System.out.println(book);
}//for
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SAX解析开始...
开始解析第1本书...
结束解析第1本书...
开始解析第2本书...
结束解析第2本书...
开始解析第3本书...
结束解析第3本书...
SAX解析结束...
遍历结果:
category:Java lang:chi title:Java多线程编程核心技术 author:高洪岩 year:2015 price:69.00
category:C++ lang:en title:Effective C++: 55 Specific Ways to Improve Your Programs and Designs author:Scott Meyers year:2006 price:58.00
category:Web lang:en title:Learning XML author:Erik T. Ray year:2016 price:39.95
|
标签:
原文地址:http://blog.csdn.net/sunnyyoona/article/details/50450797