There are many ways to parse XML documents. Apart from common Sax and Dom methods, you can also use the pull that comes with Java to parse XML documents.
1. Use the pull parser to parse XML documents
The pull parser runs in a similar way as the SAX Parser. It provides similar events, such as document start, document end, start element, and end element. You can use the parser. Next () method to enter the next element and trigger corresponding events. Various events have been sent in numbers, so you canProgramUse a switch statement to select an event and process the event accordingly.
1.1 Event Type
The pull parser provides a total of five event types:
(1) start_document
(2) start_tag Start Element
(3) Text
(4) end_tag End Element
(5) End of end_document
1.2 common methods
When you use the pull parser xmlpullparser to parse XML documents, the following common methods are used.
(1) int getattributecount (); // gets the number of attributes of the current element
(2) string getattributevalue (INT index); // get the attribute value
(3) int geteventtype (); // obtain the Event Type
(4) string getname (); // used to obtain the name of the current element in the start_tag and end_tag events.
(5) int next (); // process the next element
(6) int nexttext (); // used in the start_tag event to obtain the next text element.
1.3 create a pull parser
You can create a pull parser in the following two steps.
(1) create a pull parser factory object by calling the newinstance () method of the xmlpullparserfactory factory class.
(2) create a pull parser object by calling the newpullparser () method of the pull parser factory object.
After the pull parser object is created, you can pass in the XML document to be parsed by calling the setinput () method of the pull parser object. The setinput () method provides two reloads:
(1) void setinput (Reader in );
(2) void setinput (inputstream, string inputencoding );
1.4 instance
The followingCodeThe pull parser parses the "person. xml" document mentioned in the previous two blog posts, extracts the person information in the XML document, and stores the person Object List.
1 /* 2 * Function: Use the pull parser to parse XML documents. 3 * Param: inputstream transmits the XML document as an input stream 4 * Encoding format of inputencoding XML documents 5 * Retuen: List <person> person Object List 6 * Author: blog Park-still indifferent 7 */ 8 Public Static List <person> readxml (inputstream, string inputencoding) Throws Exception { 9 10 List <person> List = Null ; // Store all the person objects parsed 11 Person = Null ; // Stores the parsed individual person object 12 13 Xmlpullparserfactory factory = xmlpullparserfactory. newinstance (); // Create a pull parser factory 14 Xmlpullparser = factory. newpullparser (); // Create a pull parser 15 Xmlpullparser. setinput (inputstream, inputencoding ); // Input the XML document to be parsed as an input stream 16 17 Int Eventtype = xmlpullparser. geteventtype (); // Get Event Type 18 While (Eventtype! = Xmlpullparser. end_document ){ 19 Switch (Eventtype ){ 20 Case Xmlpullparser. start_document: // Document start 21 List = New Arraylist <person> (); 22 Break ; 23 Case Xmlpullparser. start_tag: // Start Element 24 If (Xmlpullparser. getname (). Equals ("person" )){ 25 Person = New Person (); 26 Int Id = integer. parseint (xmlpullparser. getattributevalue (0 )); // Obtains the attribute value of an element. 27 Person. setid (ID ); 28 } Else If (Xmlpullparser. getname (). Equals ("name" )){ 29 String name = xmlpullparser. nexttext (); // Get Element Content 30 Person. setname (name ); 31 } Else If (Xmlpullparser. getname (). Equals ("Age" )){ 32 Int Age =Integer. parseint (xmlpullparser. nexttext ()); 33 Person. setage (AGE ); 34 } 35 Break ; 36 Case Xmlpullparser. end_tag: // End Element 37 If (Xmlpullparser. getname (). Equals ("person")){ 38 List. Add (person ); 39 Person = Null ; 40 } 41 Break ; 42 } 43 Eventtype = xmlpullparser. Next (); // Generate a loop to traverse all elements 44 } 45 Return List; 46 }
Note that when using the pull parser, You need to import the kxml2-2.2.2.jar package in the project.