星空网 > 软件开发 > 操作系统

PULL解析XML的运行机制详解

PULL解析简单易上手,基本上看一遍,基本上就会解析啦,但总是感觉对PULL解析的运行机制不是很了解,就总结了以下事件驱动到底是怎么执行的。。

PULL: Android内置了PULL解析器。PULL解析器与SAX解析器类似,它提供了类似的事件,例如,开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发事件。每一种事件将作为数值代码被传送,因此使用一个switch来对感兴趣的事件进行处理。 
这也是我最喜欢的方法,简单好用。 
下面将对解析过程进行详细介绍,它到底是怎么运行的呢。 
这是一、START_DOCUMENT: 文档开始 
START_TAG : 标签开始 
TEXT : 文本 
END_DOCUMENT:文档结束 
END_TAG:标签结束 
CDSECT :CDATA sections was just read (this token is available only from nextToken()) 
在标记CDATA下,所有的标记、实体引用都被忽略,而被

<![CDATA[文本内容]]>  CDATA的文本内容中不能出现字符串“]]>”

  

,另外,CDATA不能嵌套。 
COMMENT :注释 
DOCDECL: 就是

<DOCTYPE 


IGNORABLE_WHITESPACE :可忽略的空白。在没用dtd约束文档时, IGNORABLE_WHITESPACE只会出现在根元素外面;对于有dtd约束的文档,空白由dtd约束文档定义。(dtd约束文档就是在DOCTYPE中指定的那个文件,它规定了可以在。。。。 
常有的标签就5个:START_DOCUMENT ,START_TAG, TEXT, END_DOCUMENT , END_TAG。

二、一些比较重要复杂的常见方法:结合源代码解析

1) int nextTag():

Call next() and return event if it is START_TAG or END_TAG otherwise throw an exception. It will skip whitespace TEXT before actual tag if any. 
//调用next()返回START_TAG or END_TAG这两个事件,其他抛出异常。它会跳过空白text 
本质执行过程就是这样: 
int eventType = next(); 
if(eventType == TEXT && isWhitespace()) { // skip whitespace 
eventType = next(); 

if (eventType != START_TAG && eventType != END_TAG) { 
throw new } 
return eventType;

2) String nextText():

If current event is START_TAG then if next element is TEXT then element content is returned or if next event is END_TAG then empty string is returned, otherwise exception is thrown. After calling this function successfully parser will be positioned on END_TAG. 
//当前事件是START_TAG,下一元素是text就返回它的内容,或下一个事件是END_TAG那么返回空字符串“”,其他抛出异常。调用完这个函数事件定位在END_TAG。 
The motivation for this function is to allow to parse consistently both empty elements and elements that has non empty content, for example for input: 

<tag>foo</tag><tag></tag> (which is equivalent to both input can be parsed with the same code: 

3. p.nextTag() 4. p.requireEvent(p.START_TAG, “”, “tag”); 5. String content = p.nextText(); 6. p.requireEvent(p.END_TAG, “”, “tag”);

This function together with nextTag make it very easy to parse 本质执行过程就是这样: 

if(getEventType() != START_TAG) { throw new this, null); } int eventType = next(); if(eventType == TEXT) { String result = getText(); eventType = next(); if(eventType != END_TAG) { throw new this, null); } return result; } else if(eventType == END_TAG) { return “”; } else { throw new this, null); }

3)int nextToken():

This method works similarly to next() but will expose additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE) if they are available in input. 
//这个方法和next()相似,但是会揭露其他事件类型,例如:COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE。如果它们在输入中。 
即它可以返回所有的事件类型:空白,注释,CDSECT。。等等 
注释太多,而且不常用,想研究的自己看源代码吧。

4)public int next()

//返回下一个解析事件 
Get next parsing event - element content wil be coalesced and only one TEXT event must be returned for whole element content (comments and processing instructions will be ignored and emtity references must be expanded or exception mus be thrown if entity reerence can not be exapnded). If element content is empty (content is “”) then no TEXT event will be reported. 
**另:next()与nextToken()的区别: 
next:主要是用于返回比较高层的事件的。其中包括:START_TAG, TEXT, END_TAG, END_DOCUMENT 
nextToken():返回所有事件。 
While next() provides access to high level parsing events, nextToken() allows access to lower level tokens.

5) void require(int type,String namespace,String name)

Test if the current event is of the given type and if the 
namespace and name do match. null will match any namespace and any name. If the current event is TEXT with isWhitespace()= true, and the required type is not TEXT, next () is called prior to the test. 
//测试当前事件是给定事件类型。Namespace,name是null表示任意匹配。 
如果当前事件是Text且为空,required type不是text,那么就会调用next() 

if (getEventType() == TEXT && type != TEXT && isWhitespace ()) next ();if (type != getEventType() || (namespace != null && !namespace.equals (getNamespace ())) || (name != null && !name.equals (getName ())) throw new 

三、解析过程讲解

(1) 创建PULL解析器 :两种方法实质一样: 
1.2. (2)常用方法: 
void setInput(InputStream inputstream,string inputEncoding) 该方法设置需要解析的 int getEventType() 该方法用来获得事件类型,例如START_DOCUMENT,END_DOCUMENT,START_TAG(标签开始),END_TAG(标签结束)等 
String getName() 获得标签名 
String getAttributeName(int index) 
String getAttributeValue(String namespace,String name) 这两个方法获得标签的属性,第一种方法是根据节点属性的序号来获取,第二种方法是根据节点的属性名来获取,参数namespace是命名空间,name是属性名。 
示例代码: 
这是android中的代码:

/**   * 读取RAW(在res文件夹下新建的)目录下的文件测试Pull解析

  

<?

  解析过程: 

PULL解析XML的运行机制详解images/loading.gif' data-original="http://images0.cnblogs.com/blog2015/747969/201508/052119069863476.png" />

在开始讲解执行过程之前,先说下每个标签的执行过程: 
1.START_TAG :标签开始 
2.TEXT :值 
3.END_TAG :标签结束 
4.TEXT : 对你没看错,这里也有一个TEXT,虽然我不知道它干嘛用的,但就是有。 
每个标签都是按这个过程执行的。好下面就这个例子详解: 
从图里可以看出执行过程: 
最开始是START_DOCUMENT,然后就到了<persons>标签,这时就像上边说的执行了: 
START_TAG,TEXT ,注意看图中的前五行(图中有一个错误第三行的‘也是’改为‘后是’,代码中已经改过来啦),evevtType=2(START_TAG)evevtType=4(TEXT),这时没有匹配的事件;就开始执行下一个:<person>标签,也是START_TAG后跟着TEXT,图中的6,7,8行,6行eventType=2,输出了属性id,紧跟着就是4(TEXT)。 
接下来是END_TAG ,TEXT 。图中的name=‘张超’结束后,紧跟着是4(TEXT)!!也就是</name>后又执行了个TEXT,接下来的2是<age>的开始事件。 
讲到这就应该很清楚啦,自己也可以试试,比如这么写:<name>小明</name>bbb 我已经试过啦,这个’bbb’是可以识别出来的,代码不难写加控制条件就能写出来,代码就不全贴啦,贴个代码片段:

if(n==1){              System.out.println("name后:"+parser.getText());              n=0;            }

3. 下面总结一下运行过程:

START_DOCUMENT ,START_TAG ,TEXT ,END_TAG,TEXT,START_TAG …… 
END_DOCUMENT 
就是这么执行的,总算搞定啦。。。。。。 
转发请注明出处:http://www.cnblogs.com/jycboy/
另推荐一个android

 




原标题:PULL解析XML的运行机制详解

关键词:xml

xml
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

了解商标局领导及其主要职责:https://www.kjdsnews.com/a/1359151.html
了解商标局国际注册处的位置及服务:https://www.kjdsnews.com/a/1359152.html
了解商标宣誓的用途与流程:https://www.kjdsnews.com/a/1359153.html
了解商标备案登记号的用途与流程:https://www.kjdsnews.com/a/1359154.html
了解商标国际申请的费用标准:https://www.kjdsnews.com/a/1359155.html
了解商标国际申请的几种有效途径:https://www.kjdsnews.com/a/1359156.html
永康白云风景区怎么走 白云山风景区怎么去??:https://www.vstour.cn/a/363181.html
2022世界杯门票如何买?:https://www.vstour.cn/a/363182.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流