SAX方式解析XML文件的步骤
- 获得SAX的解析器以解析XML文档,用newInstance()获得实例
语句:
SAXParserFactory SaxPF = SAXParserFactory.newInstance();
- 用解析器的newSAXParser()获得SAX分析器
语句:
SAXParser SaxP = SaxPF.newSAXParser();
- 需要创建一个SAXParser的处理程序,该程序类需要继承DefaultHandler,根据自己的需求扩充功能
MySAXHandler mysaxhandler = new MySAXHandler();
- 指定xml文件利用第三步的处理程序解析
SaxP.parse("ProfessionalBooks.xml", mysaxhandler);
具体代码
xml示例代码,在上一节(http://blog.csdn.net/qq_23473123/article/details/51396153#t1)有。
1. 主代码
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.imooc.entity.Book;
import com.imooc.handler.MySAXHandler;
public class MySAXTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 获得SAX的解析器以解析XML文档,用newInstance()获得实例
SAXParserFactory SaxPF = SAXParserFactory.newInstance();
try {
// 用解析器的newSAXParser()获得SAX分析器
SAXParser SaxP = SaxPF.newSAXParser();
// 需要创建一个SAXParser的处理程序,该类需要继承DefaultHandler,根据自己的需求扩充功能
MySAXHandler mysaxhandler = new MySAXHandler();
SaxP.parse("ProfessionalBooks.xml", mysaxhandler);
System.out.println("~!~!~!共有" + mysaxhandler.getBookList().size() + "本书");
for (Book book : mysaxhandler.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());
}
} 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();
}
}
}
2. SAXParser的处理程序
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.imooc.entity.Book;
public class MySAXHandler extends DefaultHandler {
private Book book = null;
private String value;
private ArrayList<Book> bookList = new ArrayList<Book>();
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ArrayList<Book> getBookList() {
return bookList;
}
public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}
/**
* 接收文档开始的通知。
* 默认情况下,不执行任何操作。应用程序编写者可以在子类中重写此方法,以便在文档的开始采取特定的措施(如分配树的根节点或创建输出文件)。
*
*/
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println(".........SAX开始解析xml文档.........");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println(".........SAX解析结束.........");
}
@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);
System.out.print("节点名为:" + qName);
int num = attributes.getLength();
if (num > 0) {
System.out.print("属性: ");
for (int i = 0; i < num; i++) {
System.out.print("属性名为~" + attributes.getQName(i) + " ");
System.out.print("属性值为~" + attributes.getValue(i) + " ");
if (qName.equals("book") && attributes.getQName(i).equals("id")) {
// System.out.print("书的id为:" + i + "......");
book=new Book();
book.setId(attributes.getValue(i));
}
}
System.out.println();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
// 判断是否针对一本书已经遍历结束
if (qName.equals("book")) {
bookList.add(book);
book = null;
System.out.println("======================结束遍历某一本书的内容=================");
} 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, start, length);
if (!value.trim().equals("")) {
System.out.println("节点值是:" + value);
}
}
}
3. Book类B我们这里用Book对象来保存book节点的所有信息
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;
}
}