Android uses XmlPullParser to parse XML files and xmlpullparser to parse

Source: Internet
Author: User

Android uses XmlPullParser to parse XML files and xmlpullparser to parse

Parse the following XML:

<? Xml version = "1.0" encoding = "UTF-8"?> <Rss> <sid> 77f265bb46de068e78f35afbadec62af </sid> <count> 3 </count> <control> 0 </control> <mblog> <uid> 1195224593 </uid> <favid> 3436952795 </favid> <mblogid> 5 xtaJR </mblogid> <mblogidnum> 3436952795 </mblogidnum> <mblogtype> 0 </mblogtype> <mlevel> 0 </mlevel> <feedid> 5 xtaJR </feedid> <nick> Ma yanli </nick> <portrait> http://tp2.sinaimg.cn/1195224593/50/5614100014/0 </portrait> <vip> 1 </vip> <vipsubtype> 0 </vipsubtype> <member_type> 13 </Member_type> <remark> </remark> <level> 2 </level> <rtnum> 11 </rtnum> <commentnum> 25 </commentnum> <attitudenum> 0 </ attitudenum> <attitudeid> 0 </attitudeid> <region> 0 </attitudes_status> <region> 0 </region> <mblogtypename> </mblogtypename> <visible> <type> 0 </type> <list_id> 0 </list_id> </visible> <content> the wedding ceremony is held in Beihai beautiful Beihai Park... Nice and romantic place... </Content> <pic> http://ss12.sinaimg.cn/wap240/473dae11494344debfc5b </pic> <time> 1288852274 </time> <source> MMS </source> </mblog> </rss>

Write the parsing as follows:

Public static Object [] getMBlogList (String content) throws HttpException {try {Object [] result = new Object [3]; List <MBlog> lst = new ArrayList <MBlog> (); result [1] = lst; final XmlPullParser parser = Xml. newPullParser (); parser. setInput (new StringReader (content); int type; while (type = parser. next ())! = XmlPullParser. END_DOCUMENT) {switch (type) {<strong> case XmlPullParser. START_TAG: Read the tag attribute of the outermost layer </strong> 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 Stri Ng 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 {<strong> MBlog B = new MBlog (); // read each MBlog object here </strong> try {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. ni Ck = s;} else 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. rtrootvip = (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();}}

Code http://download.csdn.net/detail/baidu_nod/7689299


Android parses XML files

Because Map <String, Object> map = new HashMap <String, Object> (); this Object is only new once, so list. add (map) is actually adding the same object in list all the time. Looking at your XML, he will add this map four times. Why is the value the last time, because this map is constantly changing during your loop, when you read the last tag, it will end, that is, you add the same map four times, this map will only generate 201204. The modification method is as follows:
Case XmlPullParser. END_TAG:
If ("person". equals (pullParser. getName ())){
List. add (map );
Map = new HashMap <String, Object> (); // very important
}
Break;
}
In this way, four maps are obtained, and you can also obtain all parsed information.

How android parses XML files in JAVA

Obviously .. Only one new map object is added, and the previous one is overwritten when values are placed in the back. A new map should be created after parsing.

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.