其中,内容处理器需要自己写。public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { // 1、创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 2、得到解析器 SAXParser sp = factory.newSAXParser(); // 3、得到读取器 XMLReader reader = sp.getXMLReader(); // 4、设置内容处理器 reader.setContentHandler(new TagValueHandler()); // 5、读取xml文档内容 reader.parse("src/com/fly/sax/book.xml"); }
二、将xml文档封装成对象的实现。//得到xml文档所有内容 class ListHandler implements ContentHandler { //解析内容 public void characters(char[] ch, int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } public void endDocument() throws SAXException { // TODO Auto-generated method stub } //解析结束标签 public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("<" + qName + ">"); } public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub } public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub } public void processingInstruction(String target, String data) throws SAXException { // TODO Auto-generated method stub } public void setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } public void startDocument() throws SAXException { // TODO Auto-generated method stub } /** * 解析开始标签名 * @param atts 带“s”,多个。 */ public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.print("<" + qName); // for(int i=0; i<atts.getLength(); i++) { //添加atts的空判断 for(int i=0; atts != null && i<atts.getLength(); i++) { //得到属性名称 String name = atts.getQName(i); //得到属性的值 String value = atts.getValue(i); System.out.print(" " + name + "=\"" + value + "\""); } System.out.println(">"); } public void startPrefixMapping(String prefix, String uri) throws SAXException { // TODO Auto-generated method stub } }
package com.fly.sax; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class Demo3 { /** * sax解析xml文档 * * @throws SAXException * @throws ParserConfigurationException * @throws IOException */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { // 1、创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 2、得到解析器 SAXParser sp = factory.newSAXParser(); // 3、得到读取器 XMLReader reader = sp.getXMLReader(); // 4、设置内容处理器 BeanListHandler handler = new BeanListHandler(); reader.setContentHandler(handler); // 5、读取xml文档内容 reader.parse("src/com/fly/sax/book.xml"); List<Book> list = handler.getBooks(); for(Book b : list) { System.out.println(b.getName()); } } } // 把xml文档中的每一本书封装到book对象中,并把对象放在list中返回 class BeanListHandler extends DefaultHandler { private List list = new ArrayList<Book>(); private String currentTag; private Book book; //解析开始标签 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTag = qName; if ("书".equals(currentTag)) { book = new Book(); } } //解析结束标签 public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals("书")) { list.add(book); book = null; } currentTag = null; } //解析内容 public void characters(char[] ch, int start, int length) throws SAXException { if("书名".equals(currentTag)) { book.setName(new String(ch, start, length)); } if("作者".equals(currentTag)) { book.setAuthor(new String(ch, start, length)); } if("价格".equals(currentTag)) { book.setPrice(new String(ch, start, length)); } } public List getBooks() { return list; } }