sax
原理 一个元素一个元素解析 解析到一个元素调用相关的方法 startDocument() 文档开始 endDocument 文档结束 startElement 元素开始 endElement元素结束 characters 文本内容
@Test public void saxParser() throws Exception{ //获取工厂 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser saxParser = parserFactory.newSAXParser(); //获取xmlReader通过这个reader可以试着 ContentHandler XMLReader xmlReader = saxParser.getXMLReader(); //给xmlReader设置contentHandler contentHandler是一个接口 里面太多的方法没实现 //不去直接实现contenthandler 而是继承它默认的实现 DefaultHandler xmlReader.setContentHandler(new Myhandler()); //解析xml文档 xmlReader.parse("book.xml"); } private class Myhandler extends DefaultHandler{ @Override public void startDocument() throws SAXException { System.out.println("文档开始"); } @Override public void endDocument() throws SAXException { System.out.println("文档结束"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("开始标签<"+qName+">"); } @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 { //文本内容 String text = new String(ch,start,length); System.out.println("文本内容"+text); } }
读出某一个元素对应的内容
@Test public void saxParserTest2() throws Exception{ SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser parser = parserFactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(new DefaultHandler(){ boolean isPrice = false; int count = 0; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("售价".equals(qName)){ isPrice = true; count++; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if("售价".equals(qName)){ isPrice = false; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if(isPrice && count == 2){ System.out.println(new String(ch,start,length)); } } }); reader.parse("book.xml"); }
解析成arraylist
@Test public void saxParse2list() throws Exception{ SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser parser = parserFactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(new DefaultHandler(){ ArrayList<Book> bookshelf = null; Book book = null; String tag = null; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("书架".equals(qName)){ //创建集合 bookshelf = new ArrayList<Book>(); }else if("书".equals(qName)){ //创建book对象 book = new Book(); }else if("书名".equals(qName)){ tag = "书名"; }else if("作者".equals(qName)){ tag = "作者"; }else if("售价".equals(qName)){ tag = "售价"; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { //每次走到结束标签 都要把tag复原为null tag = null; if("书架".equals(qName)){ for(Book book1:bookshelf){ System.out.println(book1); } }else if("书".equals(qName)){ //读到书的结束标签 把book对象添加到集合中 bookshelf.add(book); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if("书名".equals(tag)){ book.setTitle(new String(ch,start,length)); }else if("作者".equals(tag)){ book.setAuthor(new String(ch,start,length)); }else if("售价".equals(tag)){ book.setPrice(new String(ch,start,length)); } } }); reader.parse("book.xml"); }