package cn.itheima.sax;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class Demo1 {
publicstatic void main(String[] args) throws Exception {
//获取解析器工厂
SAXParserFactoryfactory = SAXParserFactory.newInstance();
//获取解析器
SAXParserparser = factory.newSAXParser();
//获取读取器
XMLReaderread = parser.getXMLReader();
//注册监听器
MyContentHandler2handler = new MyContentHandler2();
read.setContentHandler(handler);
//读取xml
read.parse("book.xml");
//System.out.println(handler.getList().size());
}
}
class MyContentHandler2 extendsDefaultHandler{
//获取第一本书书名
booleanisBook = false;
intcount = 0;
@Override//获取到开始标签
publicvoid startElement(String uri, String localName, String name,
Attributesatts) throws SAXException {
if("书名".equals(name)){
count++;
if(count==1){
isBook= true;
}
}
}
@Override//获取到标签内容
publicvoid characters(char[] ch, int start, int length)
throws SAXException {
if(isBook==true){
System.out.println(newString(ch,start,length));
}
}
@Override//获取到结束标签
publicvoid endElement(String uri, String localName, String name)
throwsSAXException {
isBook= false;
}
}
// 获取第二本书的第一个属性
class MyContentHandler1 extendsDefaultHandler {
intcount = 0;//定义一个计数器
@Override
publicvoid startElement(String uri, String localName, String name,
Attributesatts) throws SAXException {
//获取第二本书的第一个属性
if("书".equals(name)){
count++;
if(count == 1) {
//获取所有属性时,要用getLength()方法判断属性长度
for(int x = 0; x < atts.getLength(); x++) {
StringattName = atts.getQName(0);
StringattValue = atts.getValue(0);
System.out.println(attName+ ":" + attValue);
}
//获取第二本书的第一个属性
StringattName = atts.getQName(0);
StringattValue = atts.getValue(0);
System.out.println(attName+ ":" + attValue);
}
}
}
}
//将xml中的数据读到Book对象中去
class MyContentHandler extendsDefaultHandler {
ListbookList = new ArrayList();
Stringele = null;
Bookbook = null;
@Override
publicvoid startElement(String uri, String localName, String name,
Attributesatts) throws SAXException {
if("书".equals(name)){
book= new Book();
bookList.add(book);
if(atts.getValue("出版社") != null) {
book.setPublish(atts.getValue("出版社"));
}
}
if("书名".equals(name)){
ele= "书名";
}
if("作者".equals(name)){
ele= "作者";
}
if("售价".equals(name)){
ele= "售价";
}
}
publicList getList() {
returnbookList;
}
@Override
publicvoid characters(char[] ch, int start, int length)
throwsSAXException {
if("书名".equals(ele)){
Stringname = new String(ch, start, length);
book.setName(name);
}
if("作者".equals(ele)){
Stringauth = new String(ch, start, length);
book.setName(auth);
}
if("售价".equals(ele)){
Stringprice = new String(ch, start, length);
book.setName(price);
}
}
@Override
publicvoid endElement(String uri, String localName, String name)
throwsSAXException {
ele= "";
}
}
附:book.xml文件,缺Book.java文件
<?xml version="1.0"encoding="gb2312"?>
<书架>
<书 出版社="人民出版社">
<书名>Java就业培训教程</书名>
<特价>10.0元</特价>
<作者>张孝祥</作者>
<售价>39.00元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>