原文链接:https://www.f2er.com/xml/298936.htmlpackage cn.ouc.xml.sax; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.helpers.DefaultHandler; public class Sax { public static void main(String[] args) throws Exception { //获得解析工厂实例 SAXParserFactory factory = SAXParserFactory.newInstance(); //获得解析器 SAXParser parser = factory.newSAXParser(); DefaultHandler dh = new MyDefaultHandler(); //解析xml文档 parser.parse("books.xml",dh); System.out.println("done"); } }
MyDefaultHandler.java 继承DefaultHandler 类
package cn.ouc.xml.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyDefaultHandler extends DefaultHandler{ @Override public void startDocument() throws SAXException { System.out.println("文档开始"); } @Override /** * 如果xml文件使用了schema约束 <xs:element> * * uri:schema -- targetNameSpace * * localName--element * * qName---xs:element * 如果不使用 * * uri:null * * localName:null * * qName : element * * Attributes:当前元素的所有的属性的集合 */ public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException { System.out.println("元素开始" + qName + " *** " + attributes.getValue("id")); } @Override public void characters(char[] ch,int start,int length) throws SAXException { System.out.println(new String(ch,start,length)); } @Override public void endElement(String uri,String qName) throws SAXException { System.out.println("元素结束:" + qName); } @Override public void endDocument() throws SAXException { System.out.println("文档结束"); } }