标签:
一: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 }
class GetSecondNameHandler extends DefaultHandler {
private String contentTag;private int currentTag = 0;private int getTag = 2;
@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {contentTag = qName;}
@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {contentTag = null;}
@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {if ("name".equals(contentTag)) {currentTag++;if (currentTag == getTag) {System.out.println(new String(ch, start, length));}}}
}
javaweb学习总结十二(JAXP对XML文档进行SAX解析)
标签:
原文地址:http://www.cnblogs.com/warrior4236/p/5861576.html