Android Advanced XML File parsing

Source: Internet
Author: User

XML serialization is the writing of memory data to a hard disk or SD card.

How XML files are parsed:

1, Dom parsing, generate a tree structure, and all add memory, in memory modify the tree structure of the node can. But it consumes a lot of memory.

2. Sax parsing is based on event parsing. Fast, efficient, but not backwards.

3. Pull parsing based on event resolution


Pull Parse Mode:

The first step is to initialize the parser and set the data stream to be parsed. (Point to the beginning of the document now)

Step two, get the event.

The third step, the total node collection.

Fourth step, a single node.


public class Pullxmltools {public Pullxmltools () {//TODO auto-generated Constructor stub}/** * @param inputstream * Gets the XML file from the server, returns * @param encode * encoded format as a stream * @return * @throws Exception */public static LIST&LT;PERSON&G T Parsexml (InputStream InputStream, String encode) throws Exception {list<person> List = null; Person person = null;//load resolves the contents of each person node//create an XML parsing factory xmlpullparserfactory factory = Xmlpullparserfactory.newinstance ();//Get a reference to the XML parsing class Xmlpullparser parser = Factory.newpullparser ();p arser.setinput ( InputStream, encode);//Gets the type of event int eventtype = Parser.geteventtype (); while (eventtype! = xmlpullparser.end_document) { Switch (eventtype) {case XmlPullParser.START_DOCUMENT:list = new arraylist<person> (); break;case XmlPullParser.START_TAG:if ("Person", Equals (Parser.getname ())) {person = new person ();//Remove attribute value int id = integer.parseint (Parser.getattributevalue (0));p Erson.setid (ID);} else if ("name". Equals (Parser.getname ())) {String name = Parser.nextText ();//Gets the content of the node person.setname (name);} else if ("Age". Equals (Parser.getname ())) {int. = Integer.parseint (Parser.nexttext ());p erson.setage (age);} Break;case XmlPullParser.END_TAG:if ("person". Equals (Parser.getname ())) {List.add (person);p Erson = null;} break;} EventType = Parser.next ();} return list;}}

The XML file is persons person


Use Java class loading to load XML files in your project:






Android Advanced XML File parsing

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.