标签:
测试的xml数据:
<?xml version="1.0" encoding="utf-8" ?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don‘t forget the meeting!</body> </note>
新建SaxTest类继承至DefaultHandle
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import java.io.FileInputStream; import java.io.IOException; public class SaxTest 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);//每个标签开始都会触发该事件,attributes是标签的属性 } @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));//标签开始到标签结束之间的内容 } static public void main(String[] args) throws IOException, ParserConfigurationException, SAXException { String textXml = SaxTest.class.getResource("/").getPath() + "test.xml"; FileInputStream inputStream = new FileInputStream(textXml); SAXParserFactory.newInstance().newSAXParser().parse(inputStream, new SaxTest()); } }
解析结果截图:
其中characters方法需要特别注意,开始标签到结束标签的内容可能不止会触发一次characters事件,也就是说标签中的内容可能会多次触发characters事件,所以如果要获取标签内的完整内容需要特别小心,一般使用StringBuilder,如下代码。
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import java.io.FileInputStream; import java.io.IOException; public class SaxTest extends DefaultHandler { StringBuilder stringBuilder = new StringBuilder(); @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);//每个标签开始都会触发该事件,attributes是标签的属性 } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println(stringBuilder.toString().trim()); stringBuilder.delete(0, stringBuilder.length());//输出内容,并清空stringBuilder System.out.println(qName);//每个标签结束都会触发该事件 } @Override public void characters(char[] ch, int start, int length) throws SAXException { stringBuilder.append(ch, start, length); } static public void main(String[] args) throws IOException, ParserConfigurationException, SAXException { String textXml = SaxTest.class.getResource("/").getPath() + "test.xml"; FileInputStream inputStream = new FileInputStream(textXml); SAXParserFactory.newInstance().newSAXParser().parse(inputStream, new SaxTest()); } }
标签:
原文地址:http://www.cnblogs.com/jecyhw/p/4531049.html