标签:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <bookshelf> <book> <name bookname="thinkinjava">Think in Java</name> <author>Bruce Eckel</author> <price>95.00</price> </book> <book> <name>Effective Java</name> <author>Joshua Bloch</author> <price>52.00</price> </book> </bookshelf>
import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Objects; public class SaxDemo { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); /*reader.setContentHandler(new ListHandle());*/ /*reader.setContentHandler(new TagValueHandle());*/ BeanListHandle beanListHandle = new BeanListHandle(); reader.setContentHandler(beanListHandle); reader.parse("day02/src/book.xml"); List<Book> list = beanListHandle.getList(); System.out.println(list); } } // 遍历读取xml文件 class ListHandle implements ContentHandler { @Override public void setDocumentLocator(Locator locator) { } @Override public void startDocument() throws SAXException { } @Override public void endDocument() throws SAXException { } @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { } @Override public void endPrefixMapping(String prefix) throws SAXException { } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.println("<" + qName + ">"); // 标签属性 for (int i = 0; atts != null && i < atts.getLength(); i++) { String name = atts.getQName(i); String value = atts.getValue(i); System.out.println(name + " - " + value); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("</" + qName + ">"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { } @Override public void processingInstruction(String target, String data) throws SAXException { } @Override public void skippedEntity(String name) throws SAXException { } } // 获取指定标签的值 class TagValueHandle extends DefaultHandler { private String currentTag; // 当前解析的标签 private Integer currentIndex = 0; // 当前解析顺序 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTag = qName; if (currentTag.equals("author")) { currentIndex++; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { Integer index = 1; // 需要第一个标签的值 if (currentTag != null && currentTag.equals("author") && Objects.equals(currentIndex, index)) { System.out.println(new String(ch, start, length)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { currentTag = null; } } // xml对象封装为Bean class BeanListHandle extends DefaultHandler { private List<Book> list = new ArrayList<>(); private String currentTag; private Book book; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTag = qName; if (currentTag.equals("book")) { book = new Book(); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (currentTag != null && currentTag.equals("name")) { book.setName(new String(ch, start, length)); } if (currentTag != null && currentTag.equals("author")) { book.setAuthor(new String(ch, start, length)); } if (currentTag != null && currentTag.equals("price")) { book.setPrice(new String(ch, start, length)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { currentTag = null; if (qName.equals("book")) { list.add(book); book = null; } } public List<Book> getList() { return list; } }
标签:
原文地址:http://www.cnblogs.com/geb515/p/4493502.html