[Android] There are three ways to parse an XML file: Pull,dom,sam

Source: Internet
Author: User
Tags gettext

How Pull works:

The XML pull provides the start element and the end element. When an element starts, you can call parser. Nexttext extracts all character data from the XML document. The Enddocument event is automatically generated when parsing to the end of a document.

Common XML pull interfaces and classes:

Xmlpullparser: The parser is an interface for parsing functionality defined in ORG.XMLPULL.V1.

XmlSerializer: It is an interface that defines a sequence of XML information sets.

Xmlpullparserfactory: This class is used to create an XML pull parser in the Xmpull V1 API.

Xmlpullparserexception: Throws a single XML pull parser related error.

The pull parser operates in a similar manner to sax, and is an event-based pattern.

The difference is that the numbers are returned during pull parsing, and we need to get the generated events ourselves and do the corresponding operations, instead of executing our code in a way that the processor triggers an event as SAX does:

Read the declaration to XML return start_document; end return end_document; start tag return start_tag;

The end tag returns End_tag;

The code is as follows

Public list<blog> Parse (InputStream is) throws Exception {

List<blog> blogList = null;
Blog blog = null;

Create a Xmlpullparser instance from android.util.Xml
Xmlpullparser parser = Xml.newpullparser ();

Set the input stream and indicate how to encode
Parser.setinput (IS, "UTF-8");

Produce the first event
int eventtype = Parser.geteventtype ();

while (eventtype! = xmlpullparser.end_document) {

Switch (eventtype) {
Determines whether the current document start event

Case Xmlpullparser.start_document:
Initializing a collection of blogs
BlogList = new arraylist<blog> ();
Break

Determines whether the current event is a LABEL element start event
Case Xmlpullparser.start_tag:

if (Parser.getname (). Equals ("blog")) {

Determines whether the start tag element is a blog
Blog = new blog ();

} else if (Parser.getname (). Equals ("Blogtitle")) {

EventType = Parser.next ();
Get the property value of the blog tag and set the tile
Blog.setblogtile (Parser.gettext ());
} else if (Parser.getname (). Equals ("Blogtext")) {

EventType = Parser.next ();

Get the property value of the blog tag and set the text
Blog.setblogtext (Parser.gettext ());

}
Break

Determines whether the current event is a LABEL element end Event
Case Xmlpullparser.end_tag:
Determine if the tag element is a blog
if (Parser.getname (). Equals ("blog")) {
Add a blog to the Bloglist collection
Bloglist.add (blog);
blog = null;

}
Break
}
Go to the next element and trigger an event
EventType = Parser.next ();


}
return blogList;
}

[Android] There are three ways to parse an XML file: Pull,dom,sam

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.