一:JAXP使用SAX方式解析XML文件
1:dom解析与sax解析异同点
2:sax解析特点
二:代码案例
1:xml文件
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <students> 3 <student> 4 <name sid="111">李四</name> 5 <age>23</age> 6 <gender>男</gender> 7 </student> 8 <student> 9 <name sid="222">张三</name> 10 <age>21</age> 11 <gender>女</gender> 12 </student> 13 </students>
2:测试主体部分
1 @Test 2 public void testParseXml() throws ParserConfigurationException, 3 SAXException, IOException { 4 // 1:获取解析器工厂 5 SAXParserFactory factory = SAXParserFactory.newInstance(); 6 // 2:获取解析器 7 SAXParser parser = factory.newSAXParser(); 8 // 3:获取读取器 9 XMLReader reader = parser.getXMLReader(); 10 // 4:设置内容格式 11 reader.setContentHandler(new GetSecondNameHandler()); 12 // 5:解析文件 13 reader.parse("xml/exam.xml"); 14 }
a:获取整个xml文档标签的处理事件
1 // 定义一个解析全文的解析器 2 class MyHandler implements ContentHandler { 3 4 @Override 5 public void setDocumentLocator(Locator locator) { 6 7 } 8 9 @Override 10 public void startDocument() throws SAXException { 11 12 } 13 14 @Override 15 public void endDocument() throws SAXException { 16 17 } 18 19 @Override 20 public void startPrefixMapping(String prefix, String uri) 21 throws SAXException { 22 23 } 24 25 @Override 26 public void endPrefixMapping(String prefix) throws SAXException { 27 28 } 29 30 @Override 31 public void startElement(String uri, String localName, String qName, 32 Attributes atts) throws SAXException { 33 System.out.println("<" + qName + ">"); 34 // 获取元素的属性 35 for (int i = 0; i < atts.getLength(); i++) { 36 String attrName = atts.getQName(i); 37 String attrValue = atts.getValue(i); 38 System.out.println(attrName + "=" + attrValue); 39 } 40 } 41 42 @Override 43 public void endElement(String uri, String localName, String qName) 44 throws SAXException { 45 System.out.println("<" + qName + "/>"); 46 } 47 48 @Override 49 public void characters(char[] ch, int start, int length) 50 throws SAXException { 51 System.out.println(new String(ch, start, length)); 52 } 53 54 @Override 55 public void ignorableWhitespace(char[] ch, int start, int length) 56 throws SAXException { 57 58 } 59 60 @Override 61 public void processingInstruction(String target, String data) 62 throws SAXException { 63 64 } 65 66 @Override 67 public void skippedEntity(String name) throws SAXException { 68 69 } 70 71 }
b:定义“获取指定内容,获取第二个学生的名字”事件
1 class GetSecondNameHandler extends DefaultHandler { 2 3 private String contentTag; 4 private int currentTag = 0; 5 private int getTag = 2; 6 7 @Override 8 public void startElement(String uri, String localName, String qName, 9 Attributes attributes) throws SAXException { 10 contentTag = qName; 11 } 12 13 @Override 14 public void endElement(String uri, String localName, String qName) 15 throws SAXException { 16 contentTag = null; 17 } 18 19 @Override 20 public void characters(char[] ch, int start, int length) 21 throws SAXException { 22 if ("name".equals(contentTag)) { 23 currentTag++; 24 if (currentTag == getTag) { 25 System.out.println(new String(ch, start, length)); 26 } 27 28 } 29 } 30 31 }
c:将xml信息封装成javabean对象,然后放到list集合中
1 / 将xml中student元素内容封装成对象,然后放到list集合中 2 class BeanListHandler extends DefaultHandler { 3 private List<Student> list = new ArrayList<Student>(); 4 private String tagName; 5 private Student stu; 6 7 @Override 8 public void startElement(String uri, String localName, String qName, 9 Attributes attributes) throws SAXException { 10 tagName = qName; 11 if ("student".equals(tagName)) { 12 stu = new Student(); 13 } 14 } 15 16 @Override 17 public void characters(char[] ch, int start, int length) 18 throws SAXException { 19 20 if ("name".equals(tagName)) { 21 stu.setName(new String(ch, start, length)); 22 } 23 if ("age".equals(tagName)) { 24 stu.setAge(Integer.parseInt(new String(ch, start, length))); 25 } 26 if ("gender".equals(tagName)) { 27 stu.setGender(new String(ch, start, length)); 28 } 29 30 } 31 32 @Override 33 public void endElement(String uri, String localName, String qName) 34 throws SAXException { 35 if ("student".equals(qName)) { 36 list.add(stu); 37 stu = null; 38 } 39 40 tagName = null; 41 } 42 43 public List<Student> getStudents() { 44 return list; 45 } 46 47 }
测试程序代码修改为:
1 @Test 2 public void testParseXml() throws ParserConfigurationException, 3 SAXException, IOException { 4 // 1:获取解析器工厂 5 SAXParserFactory factory = SAXParserFactory.newInstance(); 6 // 2:获取解析器 7 SAXParser parser = factory.newSAXParser(); 8 // 3:获取读取器 9 XMLReader reader = parser.getXMLReader(); 10 // 4:设置内容格式 11 BeanListHandler beanListHandler = new BeanListHandler(); 12 reader.setContentHandler(beanListHandler); 13 // 5:解析文件 14 reader.parse("xml/exam.xml"); 15 16 List<Student> list = beanListHandler.getStudents(); 17 for (Student stu : list) { 18 System.out.println("name=" + stu.getName() + " age=" + stu.getAge() 19 + " gender=" + stu.getGender()); 20 } 21 }