為什么我的輸出不對,我覺得算法和老師的一樣,而且在主函數調用時,容器的長度返回值為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>();
//標識解析xml開始
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println("解析開始...");
}
public ArrayList<Books> getBooklist() {
return booklist;
}
public void setBooklist(ArrayList<Books> booklist) {
this.booklist = booklist;
}
//標識解析結束
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("解析結束!");
}
//解析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(" ? 節點名是 : ?"+qName+" ?:");
//不能直接調出節點值
// System.out.println(" ? 該節點值是 : "+attributes.getValue(q));
// ?返回值 為 ? ? 該節點值是 : null
}
//在未知屬性名的情況下遍歷屬性
int x=attributes.getLength();
for(int i=0;i<x;i++){
System.out.println("第"+(i+1)+"個屬性名: "+attributes.getQName(i));
System.out.println("第"+(i+1)+"個屬性值: "+attributes.getValue(i));
}
}
//遍歷xml結束標簽
@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("----------結束遍歷第"+q+"本書---------------");
}
//將賦值進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("容器長度 :"+(y=booklist.size()));
if(y==11){
for(int k=0;k<y;k++){
System.out.println("容器 ?:"+booklist.get(k).toString());
?}
}
value=null;
}
// 調用characters方法輸出節點值
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
value=new String(ch, start, length);
//回避空節點值
if(!value.trim().equals("")){
System.out.println(value);
}
}
}
------------------
輸出
booklist容器長度 : 0
解析開始...
第1個屬性名: id
第1個屬性值: 1
? ?節點名是 : ?name ?:冰與火之歌
book屬性 :冰與火之歌
容器長度 :1
? ?節點名是 : ?author ?:喬治馬丁
book屬性 :null
容器長度 :2
? ?節點名是 : ?year ?:2014
book屬性 :2014
容器長度 :3
? ?節點名是 : ?price ?:89
book屬性 :null
容器長度 :4
----------結束遍歷第1本書---------------
容器長度 :5
第1個屬性名: id
第1個屬性值: 2
? ?節點名是 : ?name ?:安徒生童話
book屬性 :安徒生童話
容器長度 :6
? ?節點名是 : ?year ?:2004
book屬性 :2004
容器長度 :7
? ?節點名是 : ?price ?:77
book屬性 :null
容器長度 :8
? ?節點名是 : ?language ?:English
容器長度 :9
----------結束遍歷第2本書---------------
容器長度 :10
容器長度 :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
解析結束!
2017-04-20
startElement()方法中,只有在qName.equals("book")的時候,book才實例化(? book = new Books()? ).