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>();
//创建pull解析器 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();
//判断正在接戏的元素标签是否是person 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();
//得到正在解析的元素标签名是否是person 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 }
json解析
import com.google.gson.Gson; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.tv); try { InputStream is=getAssets().open("newsList.json"); int length = is.available(); byte[] buffer=new byte[length]; is.read(buffer); String jsonStr=new String(buffer,0,length); Gson g=new Gson(); SuperJsonParse sjp=g.fromJson(jsonStr,SuperJsonParse.class); textView.setText(sjp.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }