SAX采用事件處理器的方式解析XML文檔,利用SAX解析XML文檔,涉及兩個部分:解析器和事件處理器
解析器可以使用JAXP的API進行創建,創建出SAX解析器後,就可以指定解析器去解析某個XML文檔
解析器在采用SAX方式解析某個XML文檔時,它只要解析到XML文檔的一個組成部分,都回去調用事件處理器的一個方法,解析器在調用事件處理器的方法時,會把當前XML文件內容作為方法的參數傳遞給事件處理器
事件處理器有程序員編寫,程序員通過事件處理器中方法的參數,就可以很輕松地得到SAX解析器得到的數據,從而可以決定如何對數據進行處理
通過一個實例體驗一下,本實例通過讀取一個book.xml文檔,將book中內容封裝到一個Book對象中,並將多個Book對象放入LIst中返回,實例代碼如下:
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ConcurrentMap;
-
- import javax.sql.rowset.spi.XmlReader;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
-
- import org.xml.sax.Attributes;
- import org.xml.sax.ContentHandler;
- import org.xml.sax.Locator;
- import org.xml.sax.SAXException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.helpers.DefaultHandler;
-
-
- public class SAXParesXML {
-
- /**
- * sax parse
- * @throws SAXException
- * @throws ParserConfigurationException
- * @throws IOException
- */
- public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
-
- // 創建解析工廠
- SAXParserFactory factory = SAXParserFactory.newInstance();
-
- // 得到解析器
- SAXParser sp = factory.newSAXParser();
-
- // 得到讀取器
- XMLReader reader = sp.getXMLReader();
-
- //設置內容處理器
- BeanListHandler beanListHandler = new BeanListHandler();
- reader.setContentHandler(beanListHandler);
-
- //讀取xml文檔內容
- reader.parse("src/book.xml");
-
- List<Book> books = beanListHandler.getBooks();
-
- //遍歷輸出
- //...
- }
-
- }
-
- // 把xml的每一本書封裝到Book對象中,並把多個Book對象封裝到List中返回
- class BeanListHandler extends DefaultHandler {
-
- private List books = new ArrayList();
- private String currentTag;
- private Book book;
-
- public List getBooks() {
- return books;
- }
-
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if ("book".equals(currentTag)) {
- book = new Book();
- }
- }
-
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if ("name".equals(currentTag)) {
- book.setName(new String(ch, start, length));
- }
-
- if ("author".equals(currentTag)) {
- book.setAuthor(new String(ch, start, length));
- }
-
- if ("price".equals(currentTag)) {
- book.setPrice(new String(ch, start, length));
- }
- }
-
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if (qName.equals("book")) {
- books.add(book);
- book = null;
- }
-
- currentTag = null;
- }
-
- }
Book類代碼如下:
- public class Book {
- private String name;
- private String author;
- private String price;
-
- 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 getPrice() {
- return price;
- }
- public void setPrice(String price) {
- this.price = price;
- }
-
- }
book.xml文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <bookstore>
- <book>
- <name>C++ l</name>
- <author>Lippman</author>
- <title>cpp</title>
- </book>
- <book>
- <name>C++ h</name>
- <author>houjie</author>
- <title>stl</title>
- </book>
- </bookstore>