应用 SAX 方式解析 XML

前端之家收集整理的这篇文章主要介绍了应用 SAX 方式解析 XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

分为三部分:


1、

package com;


import java.io.IOException;


import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


import org.xml.sax.SAXException;


import com.ii.SAXParserHandler;
import com.jj.Book;


public class SAXTest {


public static void main(String[] args) {
// TODO Auto-generated method stub

//获取一个SAXParseFactory的实例
SAXParserFactory factory = SAXParserFactory.newInstance();

//通过factory获取SAXParser
try {
SAXParser parser = factory.newSAXParser();

//创建factory获取SAXParser实例
SAXParserHandler handler = new SAXParserHandler();
parser.parse("books.xml",handler);
System.out.println("~~~~! 共有: " + handler.getBookList().size() + "本书");
for (Book book : handler.getBookList()) {
System.out.println(book.getId());
System.out.println(book.getName());
System.out.println(book.getAuthor());
System.out.println(book.getYear());
System.out.println(book.getPrice());
System.out.println(book.getLanguage());
System.out.println("-----------finish---------");

}
} catch (ParserConfigurationException | SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}



2、

package com.ii;


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.jj.Book;


public class SAXParserHandler extends DefaultHandler {
String value = null;
Book book = null;
private ArrayList<Book> bookList = new ArrayList<Book>();

public ArrayList<Book> getBookList() {
return bookList;
}


int bookIndex = 0;
/*
* 用来遍历XML文件的开始标签
* 用来解析xml元素的
* */
@Override
public void startElement(String uri,String localName,String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub

//调用DefaultHandler类的startElement()方法;
super.startElement(uri,localName,qName,attributes);
//开始解析book元素的属性

if(qName.equals("book")){
//创建一个book对象
book = new Book();


bookIndex++;
//已知book元素下属性名称,根据属性名称获取属性
//String value = attributes.getValue("id");
//System.out.println("book的属性值是:" + value);
System.out.println("=====================开始遍历第" + bookIndex + "本书的内容=============================");
//在不知道book元素下属性名称以及个数,如何获取属性名以及属性
int num = attributes.getLength();
for(int i = 0; i < num; i++){

System.out.print("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);

}
}

/*
* 用来标识解析的开始
* */
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("SAX解析开始!!");
super.startDocument();
}

/*
* 用来标识解析的结束
* */
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("SAX解析结束!!");
super.endDocument();
}
/*
* 用来遍历XML文件的结束标签
* */
@Override
public void endElement(String uri,String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri,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);
}
}

@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,length);
if(!value.trim().equals("")){
System.out.println("--节点值是: " + value);
}


}
}



3、


package com.jj; public class Book { private String id; private String name; private String author; private String year; private String price; private String language; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }

原文链接:https://www.f2er.com/xml/297154.html

猜你在找的XML相关文章