pull解析xml
来源:互联网 发布:nginx 配置 域名解析 编辑:程序博客网 时间:2024/05/17 23:52
在工程中新建解析xml测试文件
dashu.xml
<?xml version="1.0" encoding="UTF-8"?><user-list><user id="1"><name>张三</name><age>22</age></user><user id="2"><name>李四</name><age>28</age></user><user id="3"><name>王五</name><age>32</age></user></user-list>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView></LinearLayout>
items.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="" android:textSize="18sp" android:textColor="@android:color/holo_blue_dark"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="" android:textSize="18sp" android:textColor="@android:color/holo_blue_dark"/> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="" android:textSize="18sp" android:textColor="@android:color/holo_blue_dark" /></LinearLayout>
PullXmlTools解析工具类
package com.example.dashu_xml;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;import android.R.integer;/** * pull解析xml * */public class PullXmlTools {public PullXmlTools() {}/** * inputStream通常为服务器端获取的io流 encode编码格式 * * @throws XmlPullParserException * @throws IOException * @throws NumberFormatException * */public static List<User> parseXML(InputStream inputStream, String encode)throws XmlPullParserException, NumberFormatException, IOException {List<User> list = null;User user = null;// 装载每一个节点的内容XmlPullParserFactory factory = XmlPullParserFactory.newInstance();// 获得xml解析类引用XmlPullParser pullParser = factory.newPullParser();pullParser.setInput(inputStream, encode);// 获得事件类型int eventType = pullParser.getEventType();// 判定是否解析到文档结束while (eventType != XmlPullParser.END_DOCUMENT) {switch (eventType) {case XmlPullParser.START_DOCUMENT:// 开始文档list = new ArrayList<User>();break;case XmlPullParser.START_TAG:// 开始节点if ("user".equals(pullParser.getName())) {user = new User();int id = Integer.parseInt(pullParser.getAttributeValue(null, "id").toString());user.setId(id);}if (user != null) {if ("name".equals(pullParser.getName())) {String name = pullParser.nextText().toString();user.setName(name);}if ("age".equals(pullParser.getName())) {int age = Integer.parseInt(pullParser.nextText().toString());// 取出iduser.setAge(age);}}break;case XmlPullParser.END_TAG:// 结束节点时候if ("user".equals(pullParser.getName())) {list.add(user);user = null;}break;default:break;}eventType = pullParser.next();// 下一个解析标识}return list;}}
package com.example.dashu_xml;public class User {private int id;private int age;private String name;@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", age=" + age + "]";}public User() {}public User(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
MyAdapter数据适配器
package com.example.dashu_xml;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MyAdapter extends BaseAdapter {private List<User> list;private Context context;public MyAdapter(List<User> list, Context context) {this.list = list;this.context = context;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn list.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubif (arg1 == null) {LayoutInflater intInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);arg1 = intInflater.inflate(R.layout.items, null);}TextView textView1 = (TextView) arg1.findViewById(R.id.textView1);TextView textView2 = (TextView) arg1.findViewById(R.id.textView2);TextView textView3 = (TextView) arg1.findViewById(R.id.textView3);textView1.setText(list.get(arg0).getId() + "");textView2.setText(list.get(arg0).getName());textView3.setText(list.get(arg0).getAge() + "");return arg1;}}
MainActivity
package com.example.dashu_xml;import java.io.IOException;import java.io.InputStream;import java.util.List;import org.xmlpull.v1.XmlPullParserException;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.SimpleAdapter;import android.os.Build;public class MainActivity extends Activity {List<User> list = null;InputStream inputStream = null;ListView listView;MyAdapter myAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);inputStream = getResources().openRawResource(R.raw.dashu);try {list = PullXmlTools.parseXML(inputStream, "UTF-8");} catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}listView = (ListView) this.findViewById(R.id.listview);myAdapter=new MyAdapter(list, MainActivity.this);listView.setAdapter(myAdapter);}}
0 0
- XML解析Pull解析
- XML解析---PULL解析
- XML的pull解析
- Pull解析XML文件
- Pull详解解析xml
- Pull解析Xml方式
- Android Pull解析xml
- pull解析xml
- xml解析(PULL)
- Pull 解析xml
- android pull 解析xml
- pull解析xml
- PULL解析XML
- android PULL 解析XML
- Android Pull解析xml
- Pull解析xml文件
- Android Pull 解析 XML
- Android Pull 解析 XML
- shell用户手册
- EXCEL:批量读取EXCEL文件给指定数据库(access,sql server)
- 【并查集专题】
- ubuntu12.10安装myeclipse10.5 以及破解
- 面试经之给刚毕业的大学生和刚入门的程序员的忠告及图书推荐
- pull解析xml
- Xlib: connection to ":0.0" refused by server
- Java中的深拷贝(深复制)和浅拷贝(浅复制)
- 使用Truetype字体制作ArcGIS符号
- 九度OJ 1537:买卖股票
- “隐写术”的趋势
- 第十二届北京师范大学程序设计竞赛 B 数细菌
- 整合Spring MVC,mybatis,hibernate,freemarker框架实现的自定义注解Validator验证机制实现对敏感词过滤的代码分享
- 虚函数表