SAX解析工具- Sun公司提供的。内置在jdk中。org.xml.sax.*
SAX核心的API:
SAXParser类: 用于读取和解析xml文件对象
parse(File f,DefaultHandler dh)方法: 解析xml文件
参数一: File:表示 读取的xml文件。
参数二: DefaultHandler: SAX事件处理程序。使用DefaultHandler的子类
DefaultHandler类的API:
void startDocument() : 在读到文档开始时调用
void endDocument() :在读到文档结束时调用
void startElement(String uri,String localName,String qName,Attributes attributes) :读到开始标签时调用
void endElement(String uri,String qName) :读到结束标签时调用
void characters(char[] ch,int start,int length) : 读到文本内容时调用
@H_502_39@ //1.创建SAXParser对象 SAXParserparser=SAXParserFactory.newInstance().newSAXParser(); //2.调用parse方法 parser.parse(newFile("./src/contact.xml"),newMyDefaultHandler());1、读取XML
1.1、展示DefaultHandler的各个事件处理函数
Demo01.java
@H_502_39@packagecom.rk.xml.j_sax; importjava.io.File; importjavax.xml.parsers.SAXParserFactory; importjavax.xml.parsers.SAXParser; /** *第一个SAX读取xml文件程序 *@authorRK * */ publicclassDemo01 { publicstaticvoidmain(String[]args)throwsException { //1.创建SAXParserFactory工厂 SAXParserFactoryparserFactory=SAXParserFactory.newInstance(); //2.创建SAXParser对象 SAXParserparser=parserFactory.newSAXParser(); //3.调用parse方法 /** *参数一:xml文档 *参数二:DefaultHandler的子类 */ parser.parse(newFile("./src/animals.xml"),newMyDefaultHandler01()); System.out.println("执行结束!"); } }MyDefaultHandler01.java
1.2、将XML重新输出的示例
Demo02.java
@H_502_39@packagecom.rk.xml.j_sax; importjava.io.File; importjavax.xml.parsers.SAXParserFactory; importjavax.xml.parsers.SAXParser; publicclassDemo02 { publicstaticvoidmain(String[]args)throwsException { //1.创建SAXParserFactory工厂 SAXParserFactoryparserFactory=SAXParserFactory.newInstance(); //2.创建SAXParser对象 SAXParserparser=parserFactory.newSAXParser(); //3.创建Handler对象,并调用parse方法 MyDefaultHandler02handler=newMyDefaultHandler02(); parser.parse(newFile("./src/animals.xml"),handler); //4.结果输出 Stringcontent=handler.getContent(); System.out.println(content); } }MyDefaultHandler02.java
@H_502_39@packagecom.rk.xml.j_sax; importorg.xml.sax.Attributes; importorg.xml.sax.SAXException; importorg.xml.sax.helpers.DefaultHandler; /** *SAX处理器程序 *@authorRK * */ publicclassMyDefaultHandler02extendsDefaultHandler { //存储xml文档信息 privateStringBuildersb=newStringBuilder(); //获取xml信息 publicStringgetContent() { returnsb.toString(); } /** *开始标签 */ @Override publicvoidstartElement(Stringuri,Attributesattributes)throwsSAXException { sb.append("<"+qName); //判断是否有属性 if(attributes!=null) { for(inti=0;i<attributes.getLength();i++) { //得到属性名称 StringattrName=attributes.getQName(i); //得到属性值 StringattrValue=attributes.getValue(i); sb.append(""+attrName+"=\""+attrValue+"\""); } } sb.append(">"); } /** *结束标签 */ @Override publicvoidendElement(Stringuri,StringqName)throwsSAXException { sb.append("</"+qName+">"); } /** *文本内容 */ @Override publicvoidcharacters(char[]ch,intlength)throwsSAXException { //得到当前读取的文本 Stringcontent=newString(ch,length); sb.append(content); } }1.3、更进一步深入:XMLReader和ContentHandler
Demo03.java
@H_502_39@packagecom.rk.xml.j_sax; importjavax.xml.parsers.SAXParserFactory; importjavax.xml.parsers.SAXParser; importorg.xml.sax.XMLReader; publicclassDemo03 { publicstaticvoidmain(String[]args)throwsException { SAXParserFactoryparserFactory=SAXParserFactory.newInstance(); SAXParserparser=parserFactory.newSAXParser(); XMLReaderreader=parser.getXMLReader(); reader.setContentHandler(newMyContentHandler03()); reader.parse("./src/animals.xml"); } }MyContentHandler03.java
@H_502_39@packagecom.rk.xml.j_sax; importorg.xml.sax.Attributes; importorg.xml.sax.ContentHandler; importorg.xml.sax.Locator; importorg.xml.sax.SAXException; publicclassMyContentHandler03implementsContentHandler { @Override publicvoidsetDocumentLocator(Locatorlocator) { System.out.println("MyContentHandler03.setDocumentLocator()"); } @Override publicvoidstartDocument()throwsSAXException { System.out.println("MyContentHandler03.startDocument()"+"文档开始"); } @Override publicvoidendDocument()throwsSAXException { System.out.println("MyContentHandler03.endDocument()"+"文档结束"); } @Override publicvoidstartPrefixMapping(Stringprefix,Stringuri)throwsSAXException { System.out.println("MyContentHandler03.startPrefixMapping()"); } @Override publicvoidendPrefixMapping(Stringprefix)throwsSAXException { System.out.println("MyContentHandler03.endPrefixMapping()"); } @Override publicvoidstartElement(Stringuri,Attributesatts)throwsSAXException { System.out.println("MyContentHandler03.startElement()"+"==>StartELement:"+qName); } @Override publicvoidendElement(Stringuri,StringqName)throwsSAXException { System.out.println("MyContentHandler03.endElement()"+"==>EndElement:"+qName); } @Override publicvoidcharacters(char[]ch,intlength)throwsSAXException { Stringcontent=newString(ch,length); System.out.println("MyContentHandler03.characters()"+"==>characters:"+content); } @Override publicvoidignorableWhitespace(char[]ch,intlength)throwsSAXException { System.out.println("MyContentHandler03.ignorableWhitespace()"); } @Override publicvoidprocessingInstruction(Stringtarget,Stringdata)throwsSAXException { System.out.println("MyContentHandler03.processingInstruction()"); } @Override publicvoidskippedEntity(Stringname)throwsSAXException { System.out.println("MyContentHandler03.skippedEntity()"); } }以JAXP读取XML的DOM解析方法和SAX解析方法经常进行对比,如下图:
DOM解析模型如下:
SAX解析模型如下: