Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ PULL operation mechanism parsing XML Comments     - Oracle table space rename and delete table space (Database)

- CentOS 7.0 Experience with previous versions (Linux)

- Copy Recovery using RMAN repository development environment (Database)

- Merge sort Java implementation (Programming)

- OpenGL Programming Guide (8th edition of the original book) - compute shader (Programming)

- mysql_config_editor encryption and decryption of the new features of MySQL realization (Database)

- Make Windows boot disk to install USB in Ubuntu Linux (Linux)

- PostgreSQL 9.3.2 Json type of use (Database)

- Linux in order to make NMAP hide and seek with the firewall (Linux)

- Oracle 11g creates virtual private directory RMAN-06004 ORA-00942 error handling (Database)

- Linux FTP setting file description (Server)

- The Java ThreadLocal (Programming)

- GRUB and Linux system repair (Linux)

- Android Studio utility plug organize, create sharp artifact (Programming)

- VMware11 virtual machine Ubuntu14.10 system partition sda1 disk expansion (Linux)

- CentOS modify yum update source (Linux)

- How to find the available network adapter on Ubuntu (Linux)

- Editor of the popular Linux Gvim (Linux)

- Common DDOS attacks (Linux)

- Monitoring services are running properly and will email alert (Server)

  PULL operation mechanism parsing XML Comments
  Add Date : 2017-08-31      
  PULL resolve simple and approachable, basically read, and basically it will resolve the matter, but always feel run PULL resolution mechanism is not very understanding, to summarize the following events in the end is how to perform.

PULL: Android built PULL parser. PULL parser similar to SAX parser, which provides similar events, such as start element and end element event, use parser.next () can go to the next element and trigger events. Each event will be transmitted as a numeric code, so use a switch to handle an event of interest.

This is my favorite method, easy to use.

The following resolution process will be described in detail, which in the end is how to run it.

This is the XML Pull Parsing official website: http: //www.xmlpull.org/ inside a detailed analysis.

A, XmlPullParser common events:

START_DOCUMENT: beginning of the document

START_TAG: Start label

TEXT: Text

END_DOCUMENT: End of document

END_TAG: End tag

CDSECT: CDATA sections was just read (this token is available only from nextToken ())

In marked CDATA, all tags, entity references are ignored, but equally by the XML processor as character data view, CDATA form:

< ! [CDATA [text]]>
CDATA text can not appear in the string "]]>"

In addition, CDATA not be nested.

IGNORABLE_WHITESPACE: ignorable whitespace. When useless dtd bound document, IGNORABLE_WHITESPACE only appear outside the root element; For dtd bound document, a blank document by the dtd define constraints. (Dtd constraint document is specified in the DOCTYPE in the document, which sets out what can appear in the xml tags, and labels can appear where, etc.)
. . . .

Second, some of the more important and complex common method: combining source code analysis

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 the next () Returns START_TAG or END_TAG these two events, the other thrown. It skips blank text
During the execution of the essence is this:

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.
// Current event is START_TAG, the next element is text and returns its contents, or the next event is END_TAG then returns an empty string "", the other thrown. After calling this function in the event location 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.
During the execution of the essence is this:


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 and the next () is similar, but would reveal other types of events, for example: COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE. If they are in the input.
That is, it returns all event types: white space, comments, CDSECT. . and many more
Note too, and not used, I wanted to study the source code of their own to see it.

4) public int next ()

// Returns the next parse 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.
** Another: the difference next () and nextToken () is:
next: mainly used to return relatively high-level event. These include: START_TAG, TEXT, END_TAG, 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 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.
// Test the current event is a given event type. Namespace, name is null represent any match.
If the current event is a Text and empty, required type is not text, it will call the next ()

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 ());

Third, the resolution process to explain

(1) Create PULL parser: as the essence of two ways:

1.XmlPullParser parser = Xml.newPullParser ();

2. XmlPullParserFactory factory = XmlPullParserFactory.newInstance (); XmlPullParser parser2 = factory.newPullParser ();

(2) Common methods:

void setInput (InputStream inputstream, string inputEncoding) which is set to parse xml data stream and encoding.

int getEventType () method is used to obtain this type of event, such as START_DOCUMENT, END_DOCUMENT, START_TAG (start tag), END_TAG (end tag), etc.

String getName () to obtain the label name

String getAttributeName (int index)

String getAttributeValue (String namespace, String name) method to obtain these two attributes tag, the first method is based on the number of nodes to acquire property, the second method is based on the property to get the name of the node, the parameters namespace is namespace name is the attribute name.

Sample code:

This is the Android code:

/ **

    * Read RAW (the res folder new) Pull test file parsing xml file directory

    * After START_DOCUMENT are persons is START_TAG, later followed by TEXT

    * After END_TAG is followed by TEXT

    * If the current label 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 RAW (the res folder new) test.xml file directory

        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 are persons is START_TAG, later followed by TEXT

                        System.out.println ( "START_DOCUMENT");


                    case XmlPullParser.TEXT:

                        if (m == 1) {

                            System.out.println ( "After START_TAG is TEXT");

                            m = 0;



                    case XmlPullParser.START_TAG: // tag start

                        if ( "persons" .equals (parser.getName ())) {

                            System.out.println ( "START_DOCUMENT after the START_TAG");

                            m ++;


                        if ( "person" .equals (parser.getName ())) {

                            for (int i = 0; i
                                System.out.println ( "id:" + parser.getAttributeValue (i));

                        } Else if (( "name"). Equals (parser.getName ())) {


                            tagname = parser.nextText ();

                            System.out.println ( "name:" + tagname);

                        } Else if ( "age" .equals (parser.getName ())) {

                            tagname = parser.nextText ();

                            System.out.println ( "age:" + tagname);



                    case XmlPullParser.END_TAG:

                        if ( "person" .equals (parser.getName ())) {

                            System.out.println ( "person tag end");




                    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> Chao < / name>

    < Age> 20 < / age>

  < / Person>

  < Person id = "1">

    < Name> Bob < / name>

    < Age> 21 < / age>

  < / Person>

< / Persons>

Before you begin to explain the execution, saying the next execution of each label:
1.START_TAG: Start label
2.TEXT: Value
3.END_TAG: End tag
4.TEXT: you no wrong, there is also a TEXT, although I do not know why it's used, but it is there.
Each tag is performed by this process. Here's a good example of this breakdown:
As can be seen from the figure in the implementation process:
Starting with the START_DOCUMENT, then on to the tag, then as a top executive said:
START_TAG, TEXT, pay attention to plug the first five rows (figure there 'is' to an error in the third line 'after a' code have to turn over coming), evevtType = 2 (START_TAG), evevtType = 4 ( TEXT), then there is no matching event; to begin next: tag is after START_TAG followed TEXT, figure 6,7,8 line, line 6 eventType = 2, the output of the property id, followed by a is 4 (TEXT).
Next is END_TAG, TEXT. Figure name = 'Chao' after the end, followed by a 4 (TEXT)! ! That is, and then perform a TEXT, the next two are to start event.
It should be very clear mentioned it, they can also try, for example, so write: Bob bbb I've tried it, this is 'bbb' can be identified, the code is difficult to write uncontrolled conditions can write code is not sticking to it, posting a snippet:

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

3. The following summarize the running:


Is so executed, finally get it.
- Share Java-based multithreading file case (Programming)
- ARM assembler instruction debugging method (Programming)
- Deployment Kubernetes manage Docker example cluster on Ubuntu (Server)
- Hadoop2.6.3 build clusters and the development of MapReduce WIN7 by Eclipse on Linux demo (Server)
- Android Fragment everything you need to know (Programming)
- MySQL enabled SSD storage (Database)
- Linux environment variables inside (Linux)
- How to enable curl command HTTP2 support (Linux)
- Modular JavaScript (Programming)
- VSFTPD Security (Linux)
- Port Telnet command to detect the remote host is turned on (Linux)
- Merge sort Java implementation (Programming)
- Hadoop 2.7.1 Installation and Configuration under RedHat Linux 6.5 (Server)
- Linux 101 hack book reading notes (Linux)
- C # get the current screenshot (Programming)
- Talking about modern programming language syntax and standard library tightly bound phenomenon (Programming)
- Taught you how to build your own VPS server (Server)
- RHEL6.4 x86_64 build SVN service (Server)
- The mv command to move the directory two cases under Linux (Linux)
- Linux kernel boot to retain large memory method summary (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.