引用来自于:https://www.cnblogs.com/Qian123/p/5231303.html
https://blog.csdn.net/qq_36935755/article/details/77106322?utm_source=copy
SAXParserHandler.java
package com.suse.handler;
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;
import com.sun.xml.internal.fastinfoset.stax.events.StartElementEvent;
import com.suse.entity.Book;
public class SAXParserHandler extends DefaultHandler {
int bookIndex = 0;
String value=null;
Book book=null;
//public static ArrayList<Book> bookList=new ArrayList<>();
//让for each通过直接获取 对象名遍历,但是我们一般把成员变量设为私有,所以采用下面
private ArrayList<Book> bookList=new ArrayList<>();
public ArrayList<Book> getBookList() {//让for each 通过get方法 获得对象,这是在实际开发的习惯做法,保证安全
return bookList;
}
/*
* 用来遍历xml文件开始的标签
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
// 开始解析book元素的属性
if (qName.equals("book")) {
bookIndex++;
//创建一个book对象
book= new Book();
System.out.println("==============开始遍历第" + bookIndex
+ "本书的内容==================");
// 已知book元素下名称,根据属性名获取属性值
/*
* String value= attributes.getValue("id");
* System.out.println("book的属性值"+value);
*/
// 不知道属性名
int num = attributes.getLength();
for (int i = 0; i < num; i++) {
System.out.println("book元素的第" + (i + 1) + "属性名是"
+ attributes.getQName(i));
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);
}
}
/*
* 用来遍历xml文件开始的标签
*/
@Override
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.print("===============结束遍历" + bookIndex
+ "本书的遍历内容==================");
}
else if(qName.equals("name")) {
book.setName(value);
}
else if(qName.equals("author")) {
book.setAuthor(value);
}
else if(qName.equals("year")) {
book.setYear(value);
}
else if(qName.equals("price")) {
book.setPrice(value);
}
else if(qName.equals("language")) {
book.setLanguage(value);
}
}
// 用来标识解析开始
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println("SAX解析开始");
}
// 用来标识解析结束
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("SAX解析结束");
}
@Override
//获取文本内容,,冰与火之歌
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);
}
}
}