Android uses XmlPullParser to parse XML files

Source: Internet
Author: User

Android uses XmlPullParser to parse XML files

Parse the following XML:

 

 
 
  
   
77f265bb46de068e78f35afbadec62af
  
  
   
3
  
  
   
0
  
  
   
    
1195224593
   
   
    
3436952795
   
   
    
5 xtaJR
   
   
    
3436952795
   
   
    
0
   
   
    
0
   
   
    
5 xtaJR
   
   
    
Ma yanli
   
   
    
Http://tp2.sinaimg.cn/1195224593/50/5614100014/0
   
   
    
1
   
   
    
0
   
   
    
13
   
   
   
    
2
   
   
    
11
   
   
    
25
   0000
   
   
    
     
0
    
    
     
0
    
   
   
    
The wedding ceremony is held in the beautiful Beihai Park... Nice and romantic place...
   
   
    
Http://ss12.sinaimg.cn/wap240/473dae11494344debfc5b
   
   1288852274
   MMS
  
 

Write the parsing as follows:

 

 

Public static Object [] getMBlogList (String content) throws HttpException {try {Object [] result = new Object [3]; List
 
  
Lst = new ArrayList
  
   
(); Result [1] = lst; final XmlPullParser parser = Xml. newPullParser (); parser. setInput (new StringReader (content); int type; while (type = parser. next ())! = XmlPullParser. END_DOCUMENT) {switch (type ){
   Case XmlPullParser. START_TAG: Read the tag attribute of the outermost layer.If (parser. getName (). equals (count) {try {result [0] = new Integer (parseText (parser);} catch (Exception e) {result [0] = 0 ;}} else if (parser. getName (). equals (mblog) {MBlog mb = parseMBlog (parser); if (mb! = Null) lst. add (mb);} else if (parser. getName (). equals (relation) {result [2] = new Integer (parseText (parser);} break; default: break;} return result;} catch (NumberFormatException e) {throw new HttpException (e);} catch (XmlPullParserException e) {throw new HttpException (e);} catch (IOException e) {throw new HttpException (e );} catch (ParseException e) {throw new HttpException (e) ;}} private static String parseText (XmlPullParser parser) throws ParseException {try {int type = parser. next (); if (type = XmlPullParser. TEXT) {return replaceEntityRef (parser. getText (). trim ();} else {return ;}} catch (Exception e) {throw new ParseException (PARSE_ERROR, e) ;}} public static MBlog parseMBlog (XmlPullParser parser) throws ParseException {
   MBlog B = new MBlog (); // read each MBlog objectTry {int type; LOOP: {while (type = parser. next ())! = XmlPullParser. END_DOCUMENT) {switch (type) {case XmlPullParser. START_TAG: if (parser. getName (). equals (uid) {B. uid = parseText (parser); if (B. uid. equals () return null;} else if (parser. getName (). equals (favid) {B. favid = parseText (parser);} else if (parser. getName (). equals (mblogid) {B. mblogid = parseText (parser);} else if (parser. getName (). equals (nick) {String s = parseText (parser); B. nick = s;} e Lse if (parser. getName (). equals (portrait) {B. portrait = parseText (parser);} else if (parser. getName (). equals (vip) {B. vip = (parseText (parser ). equals (1 ))? True: false;} else if (parser. getName (). equals (content) {B. content = parseText (parser);} else if (parser. getName (). equals (rtrootuid) {B. rtrootuid = parseText (parser);} else if (parser. getName (). equals (rtrootid) {B. rtrootid = parseText (parser);} else if (parser. getName (). equals (rtrootnick) {String s = parseText (parser); B. rtrootnick = s;} else if (parser. getName (). equals (rtrootvip) {B. rtrootvi P = (parseText (parser). equals (1 ))? True: false;} else if (parser. getName (). equals (rtreason) {B. rtreason = parseText (parser);} else if (parser. getName (). equals (rtnum) {B. rtnum = Integer. parseInt (parseText (parser);} else if (parser. getName (). equals (commentnum) {B. commentnum = Integer. parseInt (parseText (parser);} else if (parser. getName (). equals (time) {B. time = new Date (Long. parseLong (parseText (parser) * 1000);} else if (parser. getName (). equals (pic) {B. pic = parseText (parser);} else if (parser. getName (). equals (source) {B. src = parseText (parser);} else if (parser. getName (). equals (longpolling) {B. longpolling = parseText (parser);} else if (parser. getName (). equals (latitude) {B. latitude = parseText (parser);} break; case XmlPullParser. END_TAG: if (parser. getName (). equals (mblog) {break LOOP;} break; default: break ;}}return B;} catch (Exception e) {throw new ParseException (PARSE_ERROR, e );}}
  
 

The following code is called in Activity:

 

 

public void parseAssertData() {InputStream is = null;try {is = this.getAssets().open(11.xml, Context.MODE_PRIVATE);int length = is.available();byte[] buffer = new byte[length];is.read(buffer);String temp = new String(buffer);try {Object[] array = ParseData.getMBlogList(temp);} catch (HttpException e) {e.printStackTrace();}} catch (IOException ex) {ex.printStackTrace();}}

 

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.