Android uses the Pull parser to parse XML files

Source: Internet
Author: User

Pull Introduction

1. There are three common XML parsing Methods: DOM, SAX, and Pull. Pull is recommended for Android systems.

2. The Pull parser is an open-source Java project. This method is used to parse XML files in the Android system. It can also be used for Java EE projects.

3. the Android SDK has integrated the Pull parser, without adding any jar files.

4. The running method of the Pull parser is similar to that of the SAX Parser, which provides the judgment of various events.

5. Official Website: http://xmlpull.org/

Use the Pull parser to parse XML files

1. Xml. newPullParser () to obtain the parser

2 parser. setInput (in, "UTF-8") sets the input stream and encoding

3. parser. next () gets the next parsing event and obtains an event code.

4. XmlPullParser defines constants to identify various parsing events

START_DOCUMENT, END_DOCUMENT, START_TAG, END_TAG, and TEXT

Example:

View plain
/**
* Receives an XML file input stream, reads all User nodes from the input stream, encapsulates the data into an object, loads the data into a List, and returns the result.
* @ Param in the input stream of the XML file
* @ Return a collection of User objects
* @ Throws Exception
*/
Public List <User> findAll (InputStream in) throws Exception {
// Obtain the Pull parser
XmlPullParser parser = Xml. newPullParser ();
// The parser loads the input stream
Parser. setInput (in, "UTF-8 ");
 
List <User> list = new ArrayList <User> ();
User user = null;
For (int event = parser. getEventType (); event! = XmlPullParser. END_DOCUMENT; event = parser. next ())
Switch (event ){
Case XmlPullParser. START_TAG:
If ("user". equals (parser. getName ())){
User = new User ();
String id = parser. getAttributeValue (0 );
User. setId (new Integer (id ));
List. add (user );
} Else if ("name". equals (parser. getName ())){
String name = parser. nextText ();
User. setName (name );
} Else if ("age". equals (parser. getName ())){
String age = parser. nextText ();
User. setAge (new Integer (age ));
}
}
 
Return list;
}
Use XmlSerializer to write XML

Use the following method to generate XML, which is similar to the XML document order.

StartDocument

StartTag

Attribute

Text

EndTag

EndDocument

View plain
/**
* Use XmlSerializer to write XML
* @ Param users User set
* @ Param out output stream
* @ Throws IOException
*/
Public void saveAllByXmlSerializer (List <User> users, OutputStream out) throws IOException {
XmlSerializer serializer = Xml. newSerializer ();
Serializer. setOutput (out, "UTF-8 ");
 
Serializer. startDocument ("UTF-8", true );
Serializer. startTag (null, "users ");
 
For (User user: users ){
Serializer. startTag (null, "user ");
Serializer. attribute (null, "id", user. getId (). toString ());
 
Serializer. startTag (null, "name ");
Serializer. text (user. getName ());
Serializer. endTag (null, "name ");
 
Serializer. startTag (null, "age ");
Serializer. text (user. getAge (). toString ());
Serializer. endTag (null, "age ");
 
Serializer. endTag (null, "user ");
}
 
Serializer. endTag (null, "users ");
Serializer. endDocument ();
}

Use StringBuilder to write XML files

The following is a mosaic made by yourself. If there are special characters in it, it will not be easy to do.

View plain
/**
* Describe several users in XML format and output them to the specified output stream.
* @ Param users User set
* @ Param out output stream
* @ Throws IOException
*/
Public void saveAll (List <User> users, OutputStream out) throws IOException {
StringBuilder sb = new StringBuilder ();
Sb. append ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
Sb. append ("<users> ");
For (User user: users ){
Sb. append ("<user id = \" "+ user. getId () +" \ "> ");
Sb. append ("<name>" + user. getName () + "</name> ");
Sb. append ("<age>" + user. getAge () + "</age> ");
Sb. append ("</user> ");
}
Sb. append ("</users> ");
Out. write (sb. toString (). getBytes ());
Out. close ();
}

Author: Fu rongkang Column"
 

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.