慕粉3170877
2016-08-02 15:17
Book类:
package com.imooc.test;
public class Book {
private String ID;
private String name;
private String author;
private String year;
private String price;
private String language;
//使用get和set方法可以使得外部使用
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
HandlerTest 类:
package com.imooc.test;
import java.util.ArrayList;
import javax.xml.stream.events.StartElement;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
//用来遍历xml文件的开始标签
public class HandlerTest extends DefaultHandler {
int bookIndex=0;
String value=null;
Book book=null;
private ArrayList<Book> bookList=new ArrayList<Book>();//保存book对象的内容
public ArrayList<Book> getBookList() {
return bookList;
}
/*public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
}*/
//用来结束xml文件的标签
/*public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
}*/
//用来标识解析开始
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("SAX解析开始!");
super.startDocument();
}
/*
* 解析xml元素
*/
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
if(qName.equals("book")){
book=new Book();
bookIndex++;
System.out.println("============下面开始遍历第"+bookIndex+"本书的内容==============");
//已知book元素下的属性名称,并由属性名称得到属性名和属性值
//String value=attributes.getValue("id");
//System.out.println("book的属性值是:"+value);
//如果不知道book属性下的属性名和属性个数
int num=attributes.getLength();
for(int i=0;i<num;i++){
System.out.print(qName+"元素的第"+(i+1)+"个属性名是:"+attributes.getQName(i));//注意两本书中都仅有ID一个属性
System.out.println("---属性值是:"+attributes.getValue(i));
if(attributes.getQName(i).equals("id")){
book.setID(attributes.getValue(i));
}
}
}else if(!(qName.equals("book"))&&!(qName.equals("bookstore"))){
System.out.print("节点名是:"+qName);
//qName.equals("name")时,就可以在book对象中setName
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
value=new String(ch, start, length);
if(!(value.trim().equals(""))){ //返回字符串的副本,忽略前导空白和尾部空白。
System.out.println("---节点值是:"+value);
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
//判断是否针对一本书已经遍历结束
if(qName.equals("book")){
bookList.add(book);
book=null;
System.out.println("============结束遍历第"+bookIndex+"本书的内容==============");
}else if(qName.equals("name")){
book.setName(value );
}else if(qName.equals("author")){
book.setName(value );
}else if(qName.equals("year")){
book.setName(value );
}else if(qName.equals("price")){
book.setName(value );
}else if(qName.equals("language")){
book.setName(value );
}
}
//用来标识解析结束
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("SAX解析结束!");
super.endDocument();
}
}
SaxTest类:
package com.imooc.test;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class SaxTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
SAXParserFactory factory=SAXParserFactory.newInstance();
try {
SAXParser parser=factory.newSAXParser();
HandlerTest handler=new HandlerTest();
parser.parse("f:\\books.xml",handler);
System.out.println("----共有"+handler.getBookList().size()+"本书-----");
for(Book book:handler.getBookList()){//遍历booList中的内容
System.out.println(book.getID());
System.out.println(book.getName());
System.out.println(book.getAuthor());
System.out.println(book.getYear());
System.out.println(book.getPrice());
System.out.println(book.getLanguage());
System.out.println("===========结束==========");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
SAX解析开始!
============下面开始遍历第1本书的内容==============
book元素的第1个属性名是:id---属性值是:1
节点名是:name---节点值是:老人与海
节点名是:author---节点值是:海明威
节点名是:year---节点值是:2015
节点名是:price---节点值是:67
============结束遍历第1本书的内容==============
============下面开始遍历第2本书的内容==============
book元素的第1个属性名是:id---属性值是:2
节点名是:name---节点值是:看见
节点名是:language---节点值是:Chinses
节点名是:author---节点值是:柴静
节点名是:year---节点值是:2014
节点名是:price---节点值是:87
============结束遍历第2本书的内容==============
SAX解析结束!
----共有2本书-----
1
67
null
null
null
null
===========结束==========
2
87
null
null
null
null
===========结束==========
if(qName.equals("book")){
bookList.add(book);
book=null;
System.out.println("============结束遍历第"+bookIndex+"本书的内容==============");
}else if(qName.equals("name")){
book.setName(value );
}else if(qName.equals("author")){
book.setName(value );
}else if(qName.equals("year")){
book.setName(value );
}else if(qName.equals("price")){
book.setName(value );
}else if(qName.equals("language")){
book.setName(value );
}
name是setName,author是setAuthor,下面的也是,要对应
Java眼中的XML---文件读取
83199 学习 · 431 问题
相似问题