SAX解析XML文档——(二)
SAX从上向下解析,一行一行解析。节省内存,不适合CRUD。
XML文档:
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名 name="xxxxx">Java高级</书名> <作者>武陟县</作者> <价格>200元</价格> </书> <书> <书名>Java中级</书名> <作者>打火机</作者> <价格>2000元</价格> </书> </书架>
--------------------------------------------解析所有的内容:-----------------
1 package XMLParse; 2 3 import javax.xml.parsers.ParserConfigurationException; 4 import javax.xml.parsers.SAXParser; 5 import javax.xml.parsers.SAXParserFactory; 6 7 import org.apache.commons.lang.Validate; 8 import org.xml.sax.Attributes; 9 import org.xml.sax.ContentHandler; 10 import org.xml.sax.Locator; 11 import org.xml.sax.SAXException; 12 import org.xml.sax.XMLReader; 13 14 public class Demo1 { 15 16 public static void main(String[] args) throws Exception { 17 // 1.创建解析工厂 18 SAXParserFactory factory = SAXParserFactory.newInstance(); 19 // 2.得到解析器 20 SAXParser sParser = factory.newSAXParser(); 21 // 3.得到读取器 22 XMLReader reader = sParser.getXMLReader(); 23 // 4.设置内容处理器 24 reader.setContentHandler(new ListHandler()); 25 // 5.读取xml文件 26 reader.parse("src/Book.xml"); 27 } 28 } 29 30 // 得到xml文档的所有内容 31 class ListHandler implements ContentHandler { 32 33 // 处理开始标签 34 @Override 35 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 36 System.out.println("<" + qName + ">"); 37 // 常用的循环方式 读取属性 38 for (int i = 0;atts!=null && i < atts.getLength(); i++) { 39 String attName = atts.getQName(i); 40 String attValue = atts.getValue(i); 41 System.out.println(attName+"="+attValue); 42 } 43 } 44 45 // 处理内容 46 @Override 47 public void characters(char[] ch, int start, int length) throws SAXException { 48 // TODO Auto-generated method stub 49 System.out.println(new String(ch, start, length)); 50 } 51 52 // 处理结束标签 53 @Override 54 public void endElement(String uri, String localName, String qName) throws SAXException { 55 System.out.println("</" + qName + ">"); 56 57 } 58 59 @Override 60 public void setDocumentLocator(Locator locator) { 61 // TODO Auto-generated method stub 62 63 } 64 65 @Override 66 public void startDocument() throws SAXException { 67 // TODO Auto-generated method stub 68 69 } 70 71 @Override 72 public void endDocument() throws SAXException { 73 // TODO Auto-generated method stub 74 75 } 76 77 @Override 78 public void startPrefixMapping(String prefix, String uri) throws SAXException { 79 // TODO Auto-generated method stub 80 81 } 82 83 @Override 84 public void endPrefixMapping(String prefix) throws SAXException { 85 // TODO Auto-generated method stub 86 87 } 88 89 @Override 90 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 91 // TODO Auto-generated method stub 92 93 } 94 95 @Override 96 public void processingInstruction(String target, String data) throws SAXException { 97 // TODO Auto-generated method stub 98 99 } 100 101 @Override 102 public void skippedEntity(String name) throws SAXException { 103 // TODO Auto-generated method stub 104 105 } 106 107 }
结果:
<书架> <书> <书名> name=xxxxx Java高级 </书名> <作者> 武陟县 </作者> <价格> 200元 </价格> </书> <书> <书名> Java中级 </书名> <作者> 打火机 </作者> <价格> 2000元 </价格> </书> </书架>
-----------------------------------------解析指定的标签------------------------------------------------------
1 package XMLParse; 2 3 import javax.xml.parsers.ParserConfigurationException; 4 import javax.xml.parsers.SAXParser; 5 import javax.xml.parsers.SAXParserFactory; 6 7 import org.apache.commons.lang.Validate; 8 import org.xml.sax.Attributes; 9 import org.xml.sax.ContentHandler; 10 import org.xml.sax.Locator; 11 import org.xml.sax.SAXException; 12 import org.xml.sax.XMLReader; 13 import org.xml.sax.helpers.DefaultHandler; 14 15 public class Demo3 { 16 17 public static void main(String[] args) throws Exception { 18 // 1.创建解析工厂 19 SAXParserFactory factory = SAXParserFactory.newInstance(); 20 // 2.得到解析器 21 SAXParser sParser = factory.newSAXParser(); 22 // 3.得到读取器 23 XMLReader reader = sParser.getXMLReader(); 24 // 4.设置内容处理器 25 reader.setContentHandler(new TagValueHandler_1()); 26 // 5.读取xml文件 27 reader.parse("src/Book.xml"); 28 } 29 } 30 31 //读取指定的标签(读取书名) 32 class TagValueHandler_1 extends DefaultHandler{ 33 34 private String currentTag; //记住当前的标签是那个 35 private int needNum = 2; //记住需要第几个 36 private int currentNum; //记住当前是第几个 37 38 @Override 39 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 40 if("书名".equals(qName)){ 41 currentTag="书名"; 42 currentNum++; 43 } 44 } 45 46 @Override 47 public void endElement(String uri, String localName, String qName) throws SAXException { 48 currentTag=null; 49 } 50 51 @Override 52 public void characters(char[] ch, int start, int length) throws SAXException { 53 if("书名".equals(currentTag)&&needNum==currentNum){ 54 System.out.println(new String(ch,start,length)); 55 } 56 } 57 58 }
结果:
Java中级
-------------------------------------解析后封装到对象------------------------------
package XMLParse; 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.apache.commons.lang.Validate; 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 Demo4 { public static void main(String[] args) throws Exception { // 1.创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 2.得到解析器 SAXParser sParser = factory.newSAXParser(); // 3.得到读取器 XMLReader reader = sParser.getXMLReader(); // 4.设置内容处理器 ListBeanHandler listBeanHandler = new ListBeanHandler(); reader.setContentHandler(listBeanHandler); // 5.读取xml文件 reader.parse("src/Book.xml"); List<Book> list = listBeanHandler.getList(); System.out.println(list); } } // 读取指定的标签(读取书名) class ListBeanHandler extends DefaultHandler { private String currentTag; // 记住当前的标签是那个 private List<Book> list = new ArrayList<Book>(); private Book book; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTag = qName; if ("书".equals(qName)) { book = new Book(); } } @Override 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)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { currentTag = null; if("书".equals(qName)){ list.add(book); book=null; } } public List<Book> getList() { return list; } public void setList(List<Book> list) { this.list = list; } }
结果:
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】