SAX
實例:
public class SAXParse {
public static void main(String[] args) {
//獲取一個SAXParserFactroy的實例
SAXParserFactory factory = SAXParserFactory.newInstance();
//通過factory獲取SAXParser實例
try {
SAXParser parser = factory.newSAXParser();
//創建SAXParserHandler對象
SAXParserHandler handler = new SAXParserHandler();
parser.parse("books.xml", handler);
System.out.println("一共有"+handler.getBookList().size()+"本書");
for(Book book : handler.getBookList()){
System.out.println("=======start======");
System.out.println(book.getId());
System.out.println(book.getName());
System.out.println(book.getAuthor());
System.out.println(book.getPrice());
System.out.println(book.getYear());
System.out.println(book.getLanguage());
System.out.println("=======end========");
}
} 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();
}
}
}
public class SAXParserHandler extends DefaultHandler {
int bookIndex = 0;
Book book =null;
String value = null;
ArrayList<Book> bookList = new ArrayList<Book>();
@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);
}
}
/**
* 用來遍歷xml結束標籤
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
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.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);
}
}
/**
* 用來遍歷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);
//qName走到的元素屬性
//開始走到了book屬性
if(qName.equals("book")){
//創建一個book對象
book = new Book();
bookIndex++;
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) +"--屬性值是:"+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+"---");
}
}
/**
* 標誌解析開始
*/
@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解析結束");
}
public ArrayList<Book> getBookList() {
return bookList;
}
public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}
}