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<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