為什么我的輸出不對(duì),我覺(jué)得算法和老師的一樣,而且在主函數(shù)調(diào)用時(shí),容器的長(zhǎng)度返回值為0
package com.imooc.sax;
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;
public class Dh ?extends DefaultHandler{
private int q=0;
Books book=null;
String value=null;
private ArrayList<Books> ?booklist=new ArrayList<Books>();
//標(biāo)識(shí)解析xml開(kāi)始
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println("解析開(kāi)始...");
}
public ArrayList<Books> getBooklist() {
return booklist;
}
public void setBooklist(ArrayList<Books> booklist) {
this.booklist = booklist;
}
//標(biāo)識(shí)解析結(jié)束
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("解析結(jié)束!");
}
//解析xml元素
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//用SAXException類的 startElement方法遍歷屬性
super.startElement(uri, localName, qName, attributes);
book=new Books();
//在已知屬性名情況下遍歷屬性
if(qName.equals("book")){
q++;
// String att=attributes.getValue("id");
// System.out.println("屬性值 :"+att);
}else if(!qName.equals("book")&&!qName.equals("bookstore")){
System.out.print(" ? 節(jié)點(diǎn)名是 : ?"+qName+" ?:");
//不能直接調(diào)出節(jié)點(diǎn)值
// System.out.println(" ? 該節(jié)點(diǎn)值是 : "+attributes.getValue(q));
// ?返回值 為 ? ? 該節(jié)點(diǎn)值是 : null
}
//在未知屬性名的情況下遍歷屬性
int x=attributes.getLength();
for(int i=0;i<x;i++){
System.out.println("第"+(i+1)+"個(gè)屬性名: "+attributes.getQName(i));
System.out.println("第"+(i+1)+"個(gè)屬性值: "+attributes.getValue(i));
}
}
//遍歷xml結(jié)束標(biāo)簽
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
//用SAXException類的 startElement方法遍歷屬性
super.endElement(uri, localName, qName);
if(qName.equals("book")){
? ? System.out.println("----------結(jié)束遍歷第"+q+"本書(shū)---------------");
}
//將賦值進(jìn)books
else if(qName.equals("id")){
book.setId(value);
System.out.println("Books的屬性 :"+book.getId());
}
else if(qName.equals("name")){
book.setName(value);
System.out.println("book屬性 :"+book.getName());
}else if(qName.equals("year")){
book.setYear(value);
System.out.println("book屬性 :"+book.getYear());
}
else if(qName.equals("author")){
book.setYear(value);
System.out.println("book屬性 :"+book.getAuthor());
}else if(qName.equals("price")){
book.setYear(value);
System.out.println("book屬性 :"+book.getPrice());
}else if(qName.equals("langguge")){
book.setYear(value);
System.out.println("book屬性 :"+book.getLangguge());
}
booklist.add(book);
int y;
System.out.println("容器長(zhǎng)度 :"+(y=booklist.size()));
if(y==11){
for(int k=0;k<y;k++){
System.out.println("容器 ?:"+booklist.get(k).toString());
?}
}
value=null;
}
// 調(diào)用characters方法輸出節(jié)點(diǎn)值
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
value=new String(ch, start, length);
//回避空節(jié)點(diǎn)值
if(!value.trim().equals("")){
System.out.println(value);
}
}
}
------------------
輸出
booklist容器長(zhǎng)度 : 0
解析開(kāi)始...
第1個(gè)屬性名: id
第1個(gè)屬性值: 1
? ?節(jié)點(diǎn)名是 : ?name ?:冰與火之歌
book屬性 :冰與火之歌
容器長(zhǎng)度 :1
? ?節(jié)點(diǎn)名是 : ?author ?:喬治馬丁
book屬性 :null
容器長(zhǎng)度 :2
? ?節(jié)點(diǎn)名是 : ?year ?:2014
book屬性 :2014
容器長(zhǎng)度 :3
? ?節(jié)點(diǎn)名是 : ?price ?:89
book屬性 :null
容器長(zhǎng)度 :4
----------結(jié)束遍歷第1本書(shū)---------------
容器長(zhǎng)度 :5
第1個(gè)屬性名: id
第1個(gè)屬性值: 2
? ?節(jié)點(diǎn)名是 : ?name ?:安徒生童話
book屬性 :安徒生童話
容器長(zhǎng)度 :6
? ?節(jié)點(diǎn)名是 : ?year ?:2004
book屬性 :2004
容器長(zhǎng)度 :7
? ?節(jié)點(diǎn)名是 : ?price ?:77
book屬性 :null
容器長(zhǎng)度 :8
? ?節(jié)點(diǎn)名是 : ?language ?:English
容器長(zhǎng)度 :9
----------結(jié)束遍歷第2本書(shū)---------------
容器長(zhǎng)度 :10
容器長(zhǎng)度 :11
容器 ?:com.imooc.sax.Books@46f5f779
容器 ?:com.imooc.sax.Books@1c2c22f3
容器 ?:com.imooc.sax.Books@18e8568
容器 ?:com.imooc.sax.Books@33e5ccce
容器 ?:com.imooc.sax.Books@33e5ccce
容器 ?:com.imooc.sax.Books@5a42bbf4
容器 ?:com.imooc.sax.Books@270421f5
容器 ?:com.imooc.sax.Books@52d455b8
容器 ?:com.imooc.sax.Books@4f4a7090
容器 ?:com.imooc.sax.Books@4f4a7090
容器 ?:com.imooc.sax.Books@4f4a7090
解析結(jié)束!
2017-04-20
startElement()方法中,只有在qName.equals("book")的時(shí)候,book才實(shí)例化(? book = new Books()? ).