Dom剖析功能强大,可增编削查,操纵时会将xml文档以文档对象的体式格局读取到内存中,因而适用于小文档
Sax剖析是从头至尾逐行逐一元素读取内容,修正较为不方便,但适用于只读的大文档
本文重要解说Sax剖析,其他放在背面
Sax采纳事宜驱动的体式格局剖析文档。简单点说,如同在影戏院看影戏一样,从头至尾看一遍就完了,不能回退(Dom可来来回回读取)
在看影戏的过程当中,每碰到一个情节,一段泪水,一次擦肩,你都邑变更大脑和神经去吸收或处置惩罚这些信息
一样,在Sax的剖析过程当中,读取到文档开首、末端,元素的开首和末端都邑触发一些回调要领,你能够在这些回调要领中进行响应事宜处置惩罚
这四个要领是:startDocument() 、 endDocument()、 startElement()、 endElement
另外,光读取到节点处是不够的,我们还须要characters()要领来细致处置惩罚元素内包括的内容
将这些回调要领鸠合起来,便形成了一个类,这个类也就是我们须要的触发器
平常从Main要领中读取文档,却在触发器中处置惩罚文档,这就是所谓的事宜驱动剖析要领
如上图,在触发器中,起首最先读取文档,然后最先逐一剖析元素,每一个元素中的内容会返回到characters()要领
接着完毕元素读取,一切元素读取完后,完毕文档剖析
如今我们最先建立触发器这个类,要建立这个类起首须要继续DefaultHandler
建立SaxHandler,并覆写响应要领:
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxHandler extends DefaultHandler { /* 此要领有三个参数 arg0是传返来的字符数组,其包括元素内容 arg1和arg2分别是数组的最先位置和完毕位置 */ @Override public void characters(char[] arg0, int arg1, int arg2) throws SAXException { String content = new String(arg0, arg1, arg2); System.out.println(content); super.characters(arg0, arg1, arg2); } @Override public void endDocument() throws SAXException { System.out.println("\n…………完毕剖析文档…………"); super.endDocument(); } /* arg0是称号空间 arg1是包括称号空间的标签,假如没有称号空间,则为空 arg2是不包括称号空间的标签 */ @Override public void endElement(String arg0, String arg1, String arg2) throws SAXException { System.out.println("完毕剖析元素 " + arg2); super.endElement(arg0, arg1, arg2); } @Override public void startDocument() throws SAXException { System.out.println("…………最先剖析文档…………\n"); super.startDocument(); } /*arg0是称号空间 arg1是包括称号空间的标签,假如没有称号空间,则为空 arg2是不包括称号空间的标签 arg3很明显是属性的鸠合 */ @Override public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { System.out.println("最先剖析元素 " + arg2); if (arg3 != null) { for (int i = 0; i < arg3.getLength(); i++) { // getQName()是猎取属性称号, System.out.print(arg3.getQName(i) + "=\"" + arg3.getValue(i) + "\""); } } System.out.print(arg2 + ":"); super.startElement(arg0, arg1, arg2, arg3); } }
XML文档:
<?xml version="1.0" encoding="UTF-8"?> <books> <book id="001"> <title>Harry Potter</title> <author>J K. Rowling</author> </book> <book id="002"> <title>Learning XML</title> <author>Erik T. Ray</author> </book> </books>
TestDemo测试类:
import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class TestDemo { public static void main(String[] args) throws Exception { // 1.实例化SAXParserFactory对象 SAXParserFactory factory = SAXParserFactory.newInstance(); // 2.建立剖析器 SAXParser parser = factory.newSAXParser(); // 3.猎取须要剖析的文档,生成剖析器,末了剖析文档 File f = new File("books.xml"); SaxHandler dh = new SaxHandler(); parser.parse(f, dh); } }
输出效果:
…………最先剖析文档………… 最先剖析元素 books books: 最先剖析元素 book id="001"book: 最先剖析元素 title title:Harry Potter 完毕剖析元素 title 最先剖析元素 author author:J K. Rowling 完毕剖析元素 author 完毕剖析元素 book 最先剖析元素 book id="002"book: 最先剖析元素 title title:Learning XML 完毕剖析元素 title 最先剖析元素 author author:Erik T. Ray 完毕剖析元素 author 完毕剖析元素 book 完毕剖析元素 books …………完毕剖析文档…………
上面的虽然准确显现了实行流程,然则输出却很乱
为了越发清楚的实行此流程,我们还能够重写SaxHandler,使其将本来的xml文档复原一遍
重写的SaxHandler类:
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxHandler extends DefaultHandler { @Override public void characters(char[] arg0, int arg1, int arg2) throws SAXException { System.out.print(new String(arg0, arg1, arg2)); super.characters(arg0, arg1, arg2); } @Override public void endDocument() throws SAXException { System.out.println("\n完毕剖析"); super.endDocument(); } @Override public void endElement(String arg0, String arg1, String arg2) throws SAXException { System.out.print("</"); System.out.print(arg2); System.out.print(">"); super.endElement(arg0, arg1, arg2); } @Override public void startDocument() throws SAXException { System.out.println("最先剖析"); String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; System.out.println(s); super.startDocument(); } @Override public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { System.out.print("<"); System.out.print(arg2); if (arg3 != null) { for (int i = 0; i < arg3.getLength(); i++) { System.out.print(" " + arg3.getQName(i) + "=\"" + arg3.getValue(i) + "\""); } } System.out.print(">"); super.startElement(arg0, arg1, arg2, arg3); } }
更多java中运用sax剖析xml的解决要领相干文章请关注ki4网!