[android]代码库
<?xml version="1.0" encoding="utf-8"?>
<students>
<student id="20110806100">
<name>小明</name>
<age>22</age>
<sex>男</sex>
</student>
<student id="20110806101">
<name>小李</name>
<age>24</age>
<sex>男</sex>
</student>
<student id="20110806102">
<name>小丽</name>
<age>21</age>
<sex>女</sex>
</student>
</students>
/////////////////////////////////////////////////////
package com.andyidea.entity;
public class Student {
private String id;
private String name;
private int age;
private String sex;
public String getId() {
return id;
}
public void setId(String 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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
///////////////////////////////////////
package com.andyidea.util;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Xml;
import com.andyidea.entity.Student;
public class ParserByPULL {
//采用XmlPullParser来解析XML文件
public static List<Student> getStudents(InputStream inStream) throws Throwable
{
List<Student> students = null;
Student mStudent = null;
//========创建XmlPullParser,有两种方式=======
//方式一:使用工厂类XmlPullParserFactory
XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullFactory.newPullParser();
//方式二:使用Android提供的实用工具类android.util.Xml
//XmlPullParser parser = Xml.newPullParser();
//解析文件输入流
parser.setInput(inStream, "UTF-8");
//产生第一个事件
int eventType = parser.getEventType();
//只要不是文档结束事件,就一直循环
while(eventType!=XmlPullParser.END_DOCUMENT)
{
switch (eventType)
{
//触发开始文档事件
case XmlPullParser.START_DOCUMENT:
students = new ArrayList<Student>();
break;
//触发开始元素事件
case XmlPullParser.START_TAG:
//获取解析器当前指向的元素的名称
String name = parser.getName();
if("student".equals(name))
{
//通过解析器获取id的元素值,并设置student的id
mStudent = new Student();
mStudent.setId(parser.getAttributeValue(0));
}
if(mStudent!=null)
{
if("name".equals(name))
{
//获取解析器当前指向元素的下一个文本节点的值
mStudent.setName(parser.nextText());
}
if("age".equals(name))
{
//获取解析器当前指向元素的下一个文本节点的值
mStudent.setAge(new Short(parser.nextText()));
}
if("sex".equals(name))
{
//获取解析器当前指向元素的下一个文本节点的值
mStudent.setSex(parser.nextText());
}
}
break;
//触发结束元素事件
case XmlPullParser.END_TAG:
//
if("student".equals(parser.getName()))
{
students.add(mStudent);
mStudent = null;
}
break;
default:
break;
}
eventType = parser.next();
}
return students;
}
}
/////////////////////////
package com.andyidea.xmlparser;
import java.io.InputStream;
import java.util.List;
import com.andyidea.entity.Student;
import com.andyidea.util.ParserByPULL;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager asset = getAssets();
try {
InputStream input = asset.open("student.xml");
List<Student> list = ParserByPULL.getStudents(input);
for (Student stu : list) {
Log.e("StudentInfo","Person ID: " + stu.getId() + ","
+ stu.getName() + ", " + stu.getAge() + ", "
+ stu.getSex());
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
by: 发表于:2018-01-10 11:15:32 顶(0) | 踩(0) 回复
??
回复评论