Parse XML file:
In Android, the common XML parsers are sax parsers, Dom parsers, and pull parsers, which I'll explain in more detail.
SAX parser:
The SAX (simple API for XML) parser is an event-based parser, and its core is the event-handling pattern, which revolves around the event source and the event handler. When an event source generates an event, a handler for the event handler is invoked, and an event can be processed. When the event source invokes a particular method in the event handler, it is also passed to the event handler's state information, so that the event handler can determine its behavior based on the event information provided.
The advantage of the SAX parser is that it has a fast parsing speed and consumes less memory. Ideal for use in Android mobile devices.
DOM Parser:
A DOM is a collection of nodes or pieces of information based on a tree structure that allows developers to traverse an XML tree and retrieve the required data using the DOM API. Parsing the structure usually requires loading the entire document and constructing the tree structure before you can retrieve and update node information.
Because the DOM is stored in memory in a tree structure, retrieval and update efficiency is higher. However, for exceptionally large documents, parsing and loading the entire document will be very resource intensive.
Pull parser:
On the Android platform, you can use the pull parser that comes with sax, Dom, and Android to parse XML files;
The pull parser provides a variety of events, uses the Parser.next () method to enter the next element and triggers the corresponding event, and the event generated by the pull parser is a number that can be handled by the switch to the event of interest; through Paser.nexttext () method to get the value of a node of the next text type;
Reading XML
The code is as follows |
Copy Code |
Xmlpullparser Pullparser = Xml.newpullparser (); Pullparser.setinput (XML, "UTF-8");//Add XML data for the cracker to parse int event = Pullparser.geteventtype ();//start reading, get event return value Pullparser.getname ()//Get node name Pullparser.getattributevalue (0);//Get the value of the first property Pullparser.nexttext ()///Get the contents of the node after the label event = Pullparser.next (); |
The parser encounters an end tag that does not automatically parse down and needs to call this method to continue execution;
Save file to XML
The code is as follows |
Copy Code |
public static void Save (list<person> persons, OutputStream OutStream) Throws Exception, IllegalStateException, IOException { XmlSerializer serializer = Xml.newserializer ();//Get serialization object for XML write information Serializer.setoutput (OutStream, "UTF-8");/set the OutputStream to write to Serializer.startdocument ("UTF-8", true);/Set Document label Serializer.starttag (NULL, "persons");/set start tag, first parameter is namespace for (person Person:persons) { Serializer.starttag (null, "person"); Serializer.attribute (NULL, "id", Person.getid (). toString ()); Serializer.starttag (NULL, "name"); Serializer.text (Person.getname ()); Serializer.endtag (NULL, "name"); Serializer.starttag (NULL, "age"); Serializer.text (Person.getage (). toString ()); Serializer.endtag (NULL, "age"); Serializer.endtag (null, "person"); } Serializer.endtag (NULL, "persons"); Serializer.enddocument (); Outstream.flush (); Outstream.close (); } |
Parsing xml
code is as follows |
copy code |
<?xml version= "1.0" encoding= "Utf-8" <books> <book> <id>1001</id> <name>thinking in Java</name> <price>80.00</price> </book> <book> <id>1002</id> <name>core java</name> <price>90.00 </price> </book> <book> <id>1003</id> <name>hello, Andriod</name> <price>100.00</price> </ Book> </books> |
Then we use the above three kinds of parsing technology to parse the document, get a List<book> object, first look at the Book.java code:
The code is as follows |
Copy Code |
Package Com.scott.xml.model; Public class Book { private int id, private String name; private float price; < br> public int getId () { return id; } public void setId (int id) { & Nbsp;this.id = ID; } public string GetName () { return name; } public void SetName (string name { this.name = name; } public float GetPrice () { return price; public void Setprice (float Price) { this.price = Price; } @Override public String toString () { return "ID: + ID +", Name: "+ name +", Price: "+ price; } } |
Finally, we're going to generate a new XML document from the data in this collection object, and the resulting XML structure is slightly different from the original document, which is the following format:
The code is as follows |
Copy Code |
<?xml version= "1.0" encoding= "UTF-8"?> <books> <book id= "1001" > <name>thinking in Java</name> <price>80.0</price> </book> <book id= "1002" > <name>core java</name> <price>90.0</price> </book> <book id= "1003" > <name>hello, andriod</name> <price>100.0</price> </book> </books> |
Next, for the introduction of the procedure, we first define a Bookparser interface for the parser, and each type of parser needs to implement this interface. The Bookparser.java code is as follows:
code is as follows |
copy code |
Package com.scott.xml.parser; Import Java.io.InputStream; Import java.util.List; Import Com.scott.xml.model.Book; Public interface Bookparser { /** * Parse input stream get book object collection * @param is * @return * @throws Exception */ public list<book> Parse (InputStream is) throws Exception; &NBSP /** * Serialization book object collection get XML form string * @param books * @return * @th Rows Exception */ public String Serialize (List<book> books) throws Exception; } |