Android xml Parsing

Source: Internet
Author: User

Android xml parsing is often used in android development to parse xml files. common xml parsing methods include SAX, Pull, and Dom. SAX features: the parsing method is event-driven with less memory usage. The parsing method is event-driven with less memory consumption, xml parsing in the android system uses this method of Dom. dom reads all files into the memory, which consumes a lot of memory. for android phones with few memory resources, this method is not recommended. Personal suggestion: Both SAX and pull are suitable for mobile, Dom is suitable for Server Parsing, and Dom may be available as the memory of mobile phones increases. An example of SAX parsing: XML: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Users> <user name = "admin0" age = "26"> root0 </user> <user name = "admin1" age = "26"> root1 </user> <user name = "admin2" age = "26"> root2 </user> <user name = "admin3" age = "26"> root3 </user> </users> SAXHandler In the src/directory: [java] package com. hualu. androidxml; import java. util. arrayList; import java. util. list; import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler; Public class SAXHandler extends DefaultHandler {private List <User> users; User user; int I = 1; @ Override public void startDocument () throws SAXException {users = new ArrayList <User> () ;}@ Override public void endDocument () throws SAXException {super. endDocument () ;}@ Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {If ("user ". equals (localName) {I = 2; user = new User (); user. setName (attributes. getValue ("name"); user. setAge (attributes. getValue ("age") ;}@ Override public void endElement (String uri, String localName, String qName) throws SAXException {if ("user ". equals (localName) {users. add (user) ;}@ Override public void characters (char [] ch, int start, int length) throws SAXException {if (I = 2) u Ser. setPassword (String. valueOf (ch, start, length); I = 1;} public List <User> getUsers () {return users ;}} SAXClient: [java] package com. hualu. androidxml; import java. io. inputStream; import java. util. list; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import android. content. context; public class SAXClient {public void parserXML (Context context) {try {SAXParse RFactory factory = SAXParserFactory. newInstance (); SAXParser parser = factory. newSAXParser (); SAXHandler handler = new SAXHandler (); InputStream file = SAXClient. class. getClassLoader (). getResourceAsStream ("user. xml "); parser. parse (file, handler); List <User> users = handler. getUsers (); for (User user: users) {System. out. println (user. getName () + ";" + user. getPassword () + ";" + user. getAge ());} Catch (Exception e) {}} specific steps: 1. instantiate a factory SAXParserFactory. 2. instantiate the SAXPraser object and create an XMLReader parser. 3. instantiate Handler, processor 4. register the parser 4. Read the file stream 5. parse the file Pull. Example: XML: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Users> <user name = "admin0" age = "26"> root0 </user> <user name = "admin1" age = "26"> root1 </user> <user name = "admin2" age = "26"> root2 </user> <user name = "admin3" age = "26"> root3 </user> </users> res/xml directory. PullXMLParser: [java] package com. hualu. androidxml; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. res. xmlResourceParser; public class PullXMLParser {private Activity activity; public PullXMLParser (Activity activity) {this. activity = activity;} public List <User> parser () {List <User> users = new ArrayList <User> (); XmlResourceParser xrParse R = activity. getResources (). getXml (R. xml. user); try {while (xrParser. getEventType ()! = XmlResourceParser. END_DOCUMENT) {if (xrParser. getEventType () = XmlResourceParser. START_TAG) {String tagName = xrParser. getName (); if ("user ". equals (tagName) {User user = new User (); user. setName (xrParser. getAttributeValue (null, "name"); user. setAge (xrParser. getAttributeValue (null, "age"); user. setPassword (xrParser. getText (); users. add (user) ;}} xrParser. next () ;};} catch (Exception e) {} Return users ;}} specific steps: 1. use Resource to obtain XML file data and instantiate an XmlResourceParser2. use the XmlResourceParser event to determine whether to parse XmL is easier than that of SAX (I personally think ).

Related Article

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.