SAX(Simple API for XML)解析器是一种基于事件的解析器,它的核心是事件处理模式,主要是围绕着事件源以及事件处理器来工作的。当事件源产生事件后,调用事件处理器相应的处理方法,一个事件就可以得到处理。在事件源调用事件处理器中特定方法的时候,还要传递给事件处理器相应事件的状态信息,这样事件处理器才能够根据提供的事件信息来决定自己的行为。

SAX解析器的优点是解析速度快,占用内存少。非常适合在Android移动设备中使用。

SAX相关类及API

DefaultHandler:是一个事件处理器,可以接收解析器报告的所有事件,处理所发现的数据。它实现了EntityResolver接口、DTDHandler接口、ErrorHandler接口和ContentHandler接口。这几个接口代表不同类型的事件处理器。

利用SAX解析XML,实际上只需要继承DefaultHandler类,然后重写几个关键性方法即可。一般我们需要重写的方法是startDocument、startElement、characters、endElement和endDocument方法。

API解释:

 1 import org.xml.sax.Attributes;
 2 import org.xml.sax.SAXException;
 3 import org.xml.sax.helpers.DefaultHandler;
 4 
 5 public class MyParser extends DefaultHandler{
 6     
 7     /**
 8      * 当文档开始解析时此函数被调用
 9      * 
10      * 通常我们需要在这里面做一些初始化的工作,比如分配某些资源
11      */
12     @Override
13     public void startDocument() throws SAXException {
14         super.startDocument();
15     }
16     
17     /**
18      *  当扫描到文档元素节点起始标志时被调用
19      *  
20      *  通常我们需要做一下标记,或者分配节点资源。
21      */
22     @Override
23     public void startElement(String uri, String localName, String qName,
24             Attributes attributes) throws SAXException {
25         super.startElement(uri, localName, qName, attributes);
26     }
27     
28     /**
29      *  扫描到元素节点中的字符串内容时调用此函数
30      *  @param ch  代表元素内容的字符数组(实测时候并不完全表示元素内容,不过这不影响)
31      *  @param start 能够使用的起始位置
32      *  @param length 能够使用的数组长度
33      */
34     @Override
35     public void characters(char[] ch, int start, int length)
36             throws SAXException {
37         super.characters(ch, start, length);
38     }
39     
40     
41     /**
42      * 扫描到元素节点结束标志时调用
43      * 
44      * 应该是最重要的一个方法。需要判断节点名作相应的数据解析。
45      * @param localName  节点名字
46      * @param qName 含限定符的节点名字
47      */
48     @Override
49     public void endElement(String uri, String localName, String qName)
50             throws SAXException {
51         super.endElement(uri, localName, qName);
52     }
53     
54     /**
55      * 扫描文档结束后调用
56      * 
57      *  如果有些资源需要释放的话,就在这里做好了。
58      */
59     @Override
60     public void endDocument() throws SAXException {
61         super.endDocument();
62     }
63     
64 }
API解释

相关文章:

  • 2021-12-26
  • 2021-12-06
  • 2021-12-06
  • 2021-08-27
  • 2021-12-06
  • 2021-10-04
  • 2021-12-06
  • 2018-04-25
猜你喜欢
  • 2021-12-16
  • 2021-12-06
  • 2021-12-06
  • 2021-12-06
  • 2021-11-18
  • 2021-12-06
  • 2021-10-04
  • 2021-10-15
相关资源
相似解决方案