Parsing XML summary using sax in Android Development

Source: Internet
Author: User

1. Resolution class:

Inherit and override defaulthandler.

  Import Java. util. arraylist; import Java. util. list; import Org. XML. sax. attributes; import Org. XML. sax. saxexception; import Org. XML. sax. helpers. defaulthandler; import android. util. log; public class employeehandler extends   defaulthandler   {Private Static final string tag =" XML "; private list 
   
     data; private employee Employee; private string pretag; private Static final string _ Employee = "employee"; Private Static final string _ name = "name"; Private Static final string _ time = "signintime "; private Static final string _ phonenumber = "phonenumber"; Public employeehandler (list 
    
      data) {super (); this. data = data;} // parse the Document Header @ override public void startdocument () throws saxexception {
    
    
  // create an array linked list with elements as objects, used to store the data parsed by XML. If (Data = NULL) Data = new arraylist 
   
     (); log. D (TAG, "startdocument -------:"); super. startdocument () ;}@ override public void enddocument () throws saxexception {log. D (TAG, "enddocument -------:"); super. enddocument ();} // end tag of the document @ override public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {log. D (TAG, "startelement -------: localname:" + localname); pretag = localname; If (_ employee. equalsignorecase (localname) {
    
  // create an object for storing values between tags. Employee = new employee ();} super. startelement (Uri, localname, QNAME, attributes) ;}@ override public void endelement (string Uri, string localname, string QNAME) throws saxexception {pretag = ""; if (_ employee. equalsignorecase (localname) {data. add (employee); log. D (TAG, "endelement -------: tags");} super. endelement (Uri, localname, QNAME);} // The main Parsing is here @ override public void characters (char [] CH, int start, int length) throws saxexception {string info = new string (CH, start, length); log. D (TAG, "characters -------: property value:" + info);  
// Store the values in tags into the object field if (pretag. equalsignorecase (_ name) {employee. setname (Info);} else if (pretag. equalsignorecase (_ phonenumber) {employee. setphonenumber (Info);} else if (pretag. equalsignorecase (_ time) {employee. settime (Info);} super. characters (CH, start, length);} public list <employee> getdata () {return data ;}}
 
 
 
2. employeexmlhelper class:
 
The pre-and post-processing logic required for parsing is encapsulated into a tool class for convenient calling.
Import Java. io. bytearrayinputstream; import Java. io. inputstream; import Java. util. arraylist; import Java. util. list; import javax. XML. parsers. saxparserfactory; import Org. XML. sax. inputsource; import Org. XML. sax. xmlreader; public class Employeexmlhelper {Public static List <employee> Parserxml (string Str) {If (null = Str) {return NULL;} string _ STR = Str. trim (); List <employee> DATA = new arraylist <employee> (); saxparserfactory factory = saxparserfactory. newinstance (); xmlreader reader; try {byte [] bytes = _ Str. getbytes (); bytearrayinputstream Bis = new bytearrayinputstream (bytes); inputsource is = new inputsource (inputstream) bis); is. setencoding ("UTF-8"); reader = factory. newsaxparser (). getxmlreader (); reader. setcontenthandler (New Employeehandler (Data); reader. parse (is);} catch (exception e) {e. printstacktrace () ;}return data ;}}
 
 

3. Model class

 
 
 
 
Package com. Swift. phoneattendance. Model; import java. Io. serializable; public class Employee Implements serializable {  String name = NULL;  String phonenumber = NULL;  String time = NULL;  Public String getname (){ Return name;  }  Public void setname (string name ){  This. Name = Name;  }  Public String getphonenumber (){  Return phonenumber;  }  Public void setphonenumber (string phonenumber ){  This. phonenumber = phonenumber;  }  Public String gettime (){  Return time;  }  Public void settime (string time ){  This. Time = time;  }}
 
4. Client call method:
 
The XML content is as follows:
<Result><Status> true <status><Employee> <Name> </Name> <phonenumber> </employee><Employee> </result>
 
Call parsing:
 
Private list <employee> DATA = employeexmlhelper. parserxml (XML );

 
 
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.