The pull parser is an open-source Java project that can be used for both Android and javaee. If it is used in javaee, you need to put its JAR file into the class path. Because android has already been integrated into the pull parser, no JAR file needs to be added. XML files used by the Android system are parsed using the pull parser. The Running Method of the pull parser is similar to that of the SAX Parser. It provides similar events, such as the start element and end element events. You can use parser. Next () to enter the next element and trigger the corresponding event. Unlike sax, the events generated by the pull parser are numbers rather than methods. Therefore, you can use a switch to process events of interest. When parsing an element, call parser. nexttext () to obtain the value of the next text node.
The XML file to be parsed is as follows:
File Name: persons. xml
<?xml version="1.0" encoding="UTF-8"?><persons> <person id=“18"> <name>allen</name> <age>36</age> </person> <person id=“28"> <name>james</name> <age>25</age> </person></persons>
The example defines a Javabean to store the XML content parsed above. The Javabean is person, as follows:
public class Person {private Integer id;private String name;private Short age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Short getAge() {return age;}public void setAge(Short age) {this.age = age;}}
The code for reading itcast. XML using the pull parser is as follows:
Import Org. xmlpull. v1.xmlpullparser; import android. util. XML; import CN. itcast. XML. domain. person; public class pullxmlreader {public static list <person> readxml (inputstream instream) {xmlpullparser parser = xml. newpullparser (); try {parser. setinput (instream, "UTF-8"); int eventtype = parser. geteventtype (); person currentperson = NULL; List <person> Persons = NULL; while (eventtype! = Xmlpullparser. end_document) {Switch (eventtype) {Case xmlpullparser. start_document: // document start event, which can be initialized and processed by persons = new arraylist <person> (); break; Case xmlpullparser. start_tag: // start element event string name = parser. getname (); If (name. repeated signorecase ("person") {currentperson = new person (); currentperson. setid (New INTEGER (parser. getattributevalue (null, "ID");} else if (currentperson! = NULL) {If (name. equalsignorecase ("name") {currentperson. setname (parser. nexttext (); // if it is followed by a text node, return its value} else if (name. specified signorecase ("Age") {currentperson. setage (new short (parser. nexttext () ;}} break; Case xmlpullparser. end_tag: // End Element event if (parser. getname (). equalsignorecase ("person") & currentperson! = NULL) {persons. add (currentperson); currentperson = NULL;} break;} eventtype = parser. next ();} instream. close (); return persons;} catch (exception e) {e. printstacktrace ();} return NULL ;}}
Use the pull parser to generate an XML file
Sometimes, we need to generate an XML file. There are many ways to generate an XML file. For example, we can use only one stringbuilder to group XML content and then write the content into the file; you can also use the pull parser to generate xml files by using Dom APIs. We recommend that you use the pull parser.
Use the pull parser to generate a file with persons. xml
Use the following code to generate an XML file ):
File xmlfile = new file ("persons. xml ");
Fileoutputstream outstream = new fileoutputstream (xmlfile );
Outputstreamwriter outstreamwriter = new outputstreamwriter (outstream, "UTF-8 ");
Bufferedwriter writer = new bufferedwriter (outstreamwriter );
Writexml (persons, writer );
Writer. Flush ();
Writer. Close ();
If you only want to get the generated XML string content, you can use stringwriter:
Stringwriter writer = new stringwriter ();
Writexml (persons, writer );
String content = writer. tostring ();