码迷,mamicode.com
首页 > 其他好文 > 详细

XMLPuLL解析

时间:2016-01-10 20:03:23      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.bawei.day14_xmlpull;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 import org.xmlpull.v1.XmlPullParser;
  9 import org.xmlpull.v1.XmlPullParserException;
 10 
 11 import android.os.Bundle;
 12 import android.app.Activity;
 13 import android.util.Xml;
 14 import android.view.Menu;
 15 import android.widget.ArrayAdapter;
 16 import android.widget.ListView;
 17 import android.widget.TextView;
 18 
 19 public class MainActivity extends Activity {
 20 
 21     private ListView mStuListView;
 22     private List<Student> stus;
 23 
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_main);
 28         TextView tv=(TextView) findViewById(R.id.tv);
 29         mStuListView = (ListView) findViewById(R.id.stuList);
 30         try {
 31         parseStudent(getAssets().open("persons.xml"));
 32             if (stus != null && stus.size() > 0) {
 33                 ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this,    android.R.layout.simple_list_item_1, stus);
 34                 mStuListView.setAdapter(adapter);
 35             }
 36             
 37         } catch (IOException e) {
 38             // TODO Auto-generated catch block
 39             e.printStackTrace();
 40         }
 41 
 42     }
 43 
 44     private List<Student> parseStudent(InputStream is) {
 45         stus = new ArrayList<Student>();
 46         XmlPullParser parser = Xml.newPullParser();
 47         try {
 48             parser.setInput(is, "utf-8");
 49             int eventType = parser.getEventType();
 50 
 51             String tagName = "";
 52             Student stu = null;
 53             while (eventType != XmlPullParser.END_DOCUMENT) {
 54                 switch (eventType) {
 55                 case XmlPullParser.START_DOCUMENT:
 56                     break;
 57                 case XmlPullParser.START_TAG:
 58                     tagName = parser.getName();
 59                     if ("person".equals(tagName)) {
 60                         stu = new Student();
 61                         stu.setId(Integer.valueOf(parser.getAttributeValue(0)));
 62 
 63                     }
 64                     break;
 65                 case XmlPullParser.TEXT:
 66                     String text = parser.getText();
 67                     if (stu != null) {
 68                         if ("name".equals(tagName)) {
 69                             stu.setName(text);
 70                         } else if ("age".equals(tagName)) {
 71                             stu.setAge(Integer.parseInt(text));
 72                         }
 73                     }
 74                     break;
 75                 case XmlPullParser.END_TAG:
 76                     tagName = parser.getName();
 77                     if ("person".equals(tagName)) {
 78                         stus.add(stu);
 79                         stu = null;
 80 
 81                     }
 82 
 83                     tagName = "";
 84                     break;
 85 
 86                 }
 87                 eventType = parser.next();
 88             }
 89             
 90         } catch (XmlPullParserException e) {
 91             // TODO Auto-generated catch block
 92             e.printStackTrace();
 93         } catch (IOException e) {
 94             // TODO Auto-generated catch block
 95             e.printStackTrace();
 96         }finally{
 97             
 98                 try {
 99                     if(is!=null){
100                     is.close();
101                     }
102                 } catch (IOException e) {
103                     // TODO Auto-generated catch block
104                     e.printStackTrace();
105                 }
106             
107         }
108 
109         return stus;
110     }
111 
112 }

 

XMLPuLL解析

标签:

原文地址:http://www.cnblogs.com/1426837364qqcom/p/5118985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!