Detailed explanation of the running mechanism of PULL parsing XML, and the running mechanism of pullxml

Source: Internet
Author: User

Detailed explanation of the running mechanism of PULL parsing XML, and the running mechanism of pullxml

PULL Parsing is simple and easy to use. It can be parsed basically once, but I always feel that I am not very familiar with the running mechanism of PULL parsing, this article summarizes how the following event-driven processes ..

PULL: Android has a built-in PULL parser. Similar to the SAX parser, the PULL parser provides similar events, such as starting element events and ending element events. You can use parser. next () to enter the next element and trigger the event. Each event is transmitted as a numerical code, so a switch is used to process events of interest.
This is my favorite method, which is easy to use.
The following describes in detail how the parsing process runs.
This is XML Pull Parsing Official Website: http://www.xmlpull.org/inside a detailed analysis.
I. XmlPullParser common events:
START_DOCUMENT: Document start
START_TAG: Label Start
TEXT: TEXT
END_DOCUMENT: End of the document
END_TAG: tag end
CDSECT: CDATA sections was just read (this token is available only from nextToken ())
When marking CDATA, all tags and entity references are ignored, and XML processing programs treat them as character data equally. The form of CDATA is as follows:

<! [CDATA [text content]> the CDATA text content cannot contain the string "]>"

  

In addition, CDATA cannot be nested.
COMMENT: COMMENT
DOCDECL: Yes

<DOCTYPE 


IGNORABLE_WHITESPACE: Negligible blank. When no dtd constraint document is used, IGNORABLE_WHITESPACE only appears outside the root element. for documents with dtd constraints, blank spaces are defined by the dtd constraint document. (The dtd constraint document is the file specified in DOCTYPE. It specifies what labels can appear in xml and where labels can appear)
....
There are five common tags: START_DOCUMENT, START_TAG, TEXT, END_DOCUMENT, and END_TAG.

2. Some important and complex common methods: Combined with source code parsing 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.
// Call next () to return the START_TAG or END_TAG events. The other events throw an exception. It skips the blank text
The essential execution process is as follows:
Int eventType = next ();
If (eventType = TEXT & isWhitespace () {// skip whitespace
EventType = next ();
}
If (eventType! = START_TAG & eventType! = END_TAG ){
Throw new XmlPullParserException ("expected start or end tag", this, null );
}
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.
// The current event is START_TAG. If the next element is text, its content is returned. or if the next event is END_TAG, an empty string "" is returned. Other exceptions are thrown. After calling this function event, locate it at 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 XML that has no mixed content.
The essential execution process is as follows:

if(getEventType() != START_TAG) { throw new XmlPullParserException( “parser must be on START_TAG to read next text”, this, null); } int eventType = next(); if(eventType == TEXT) { String result = getText(); eventType = next(); if(eventType != END_TAG) { throw new XmlPullParserException( “event TEXT it must be immediately followed by END_TAG”, this, null); } return result; } else if(eventType == END_TAG) { return “”; } else { throw new XmlPullParserException( “parser must be on START_TAG or TEXT to read text”, 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.
// This method is similar to next (), but other event types are exposed, such as COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE. If they are in the input.
That is, it can return all event types: blank, comment, and CDSECT .. And so on.
There are too many comments, and they are not commonly used. If you want to study the source code, check the source code.

4) public int next ()

// Return the next parsing event
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.
** Differences between next () and nextToken:
Next: Mainly used to return high-level events. Including START_TAG, TEXT, END_TAG, and END_DOCUMENT.
NextToken (): returns all events.
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
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.
// Test whether the current event is of the specified event type. Namespace, whose name is null indicates any matching.
If the current event is Text and empty, and required type is not text, next () is called ()

if (getEventType() == TEXT && type != TEXT && isWhitespace ()) next ();if (type != getEventType() || (namespace != null && !namespace.equals (getNamespace ())) || (name != null && !name.equals (getName ())) throw new XmlPullParserException ( “expected “+ TYPES[ type ]+getPositionDesctiption());
Iii. Explanation

(1) create a PULL Parser: the two methods are essentially the same:
1. XmlPullParser parser = Xml. newPullParser ();
2. XmlPullParserFactory factory = XmlPullParserFactory. newInstance (); XmlPullParser parser2 = factory. newPullParser ();
(2) common methods:
Void setInput (InputStream inputstream, string inputEncoding) This method sets the xml data stream to be parsed and the encoding.
Int getEventType () This method is used to obtain the event type, such as START_DOCUMENT, END_DOCUMENT, START_TAG (TAG start), END_TAG (tag end), etc.
String getName () to obtain the Tag Name
String getAttributeName (int index)
The String getAttributeValue (String namespace, String name) methods are used to obtain the attributes of a tag. The first method is to obtain the attributes of a tag Based on the serial number of the node attribute, and the second method is to obtain the attributes of a node based on the attribute name of the node, the namespace parameter is the namespace and name is the attribute name.
Sample Code:
This is the code in android:

/*** Read the files in the RAW directory (created in the res folder) and test Pull parsing the xml file * START_DOCUMENT. persons is START_TAG, followed by TEXT * END_TAG followed by TEXT * if the current tag is START_TAG, then getTExt () returns null */private void getXmlData2 () {String tagname = null; int m = 0, n = 0; InputStream is = this. getResources (). openRawResource (R. raw. test); // read the RAW (created in the res folder) Directory test. xml file try {// create XmlPullParser XmlPullParserFactory factory = XmlPullParserFactory. newInstance (); Factory. setNamespaceAware (true); XmlPullParser parser = factory. newPullParser (); parser. setInput (is, "UTF-8"); int event = parser. getEventType (); while (event! = XmlPullParser. END_DOCUMENT) {switch (event) {case XmlPullParser. START_DOCUMENT: // After START_DOCUMENT, persons is START_TAG, followed by TEXT System. out. println ("START_DOCUMENT"); break; case XmlPullParser. TEXT: if (m = 1) {System. out. println ("TEXT after START_TAG"); m = 0;} break; case XmlPullParser. START_TAG: // tag start if ("persons ". equals (parser. getName () {System. out. println ("START_DOCUMENT followed by START_TAG"); m ++;} if ("person ". equals (parser. getName () {for (int I = 0; I <parser. getAttributeCount (); I ++) System. out. println ("id:" + parser. getAttributeValue (I);} else if ("name "). equals (parser. getName () {// System. out. println ("name:" + parser. getText (); // null tagname = parser. nextText (); System. out. println ("name:" + tagname);} else if ("age ". equals (parser. getName () {tagname = parser. nextText (); System. out. println ("age:" + tagname);} break; case XmlPullParser. END_TAG: if ("person ". equals (parser. getName () {System. out. println ("End of person tag");} break;} event = parser. next (); System. out. println ("eventType:" + event); // 2 is START_TAG, 4 is TEXT} catch (NumberFormatException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (XmlPullParserException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}

Xml source file:

<? Xml version = "1.0" encoding = "UTF-8"?> <Persons> <person id = "0"> <name> Zhang Chao </name> <age> 20 </age> </person> <person id = "1"> <name> James </name> <age> 21 </age> </person> </persons>

Resolution Process:

Before starting the execution process, let's talk about the execution process of each Tag:
1. START_TAG: Label Start
2. TEXT: Value
3. END_TAG: tag end
4. TEXT: You are not mistaken. There is also a TEXT here. Although I don't know what it is for, there is.
Each tag is executed in this process. The following is an example:
The execution process is as follows:
START_DOCUMENT first, and then<persons>Tag, as shown above:
START_TAG, TEXT. Check the first five rows in the figure. (In the figure, there is an error. The third line is also changed to "followed by", and the code has been changed ),evevtType=2(START_TAG),evevtType=4(TEXT)In this case, no matching event is found. Then, execute the following:<person>Tag, tooSTART_TAGFollowedTEXT, Lines 6, 7, 8, and 6 eventType = 2 in the figure. The property id is output, followed by 4 (TEXT ).
Next is END_TAG and TEXT. The name = 'zhangchao 'in the figure is followed by 4 (TEXT )!! That is</name>Then a TEXT is executed, and the next 2 is<age>.
It should be clear that you can try it yourself, for example:<name>James</name>I have already tried bbb. This 'bbb 'can be identified, and the code can be easily written by adding control conditions. The Code will not be completely pasted with a code snippet:

If (n = 1) {System. out. println ("name:" + parser. getText (); n = 0 ;}
3. The following is a summary of the running process:

START_DOCUMENT, START_TAG, TEXT, END_TAG, TEXT, START_TAG ......
END_DOCUMENT
That's how it works. It's done ......
Please indicate the source of forwarding: http://www.cnblogs.com/jycboy/
Another recommended android xml parsing blog, inside the XmlPullParser method to speak more comprehensive, interested in the 'can go to see: http://yuanzhifei89.iteye.com/blog/1149151

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.