在res目录下新建一个raw文件夹,在其中添加文件persons.xml, 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id ="1010">
<name>A</name>
<age>10</age>
</person>
<person id ="1111">
<name>B</name>
<age>11</age>
</person>
</persons><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/content" />
</LinearLayout>public class Person {
private Integer id;
private String name;
private Integer age;
public void setId(Integer id) {
this.id = id;
}
public void setAge(Integer age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class PersonService {
/**
* 获取对象列表
*
* @param inStream xml文件输入流
* @return 对象列表
* @throws Exception
*/
public List<Person> getPersons(InputStream inStream) throws Exception {
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(inStream, "UTF-8");
int eventType = parser.getEventType();
Person currentPerson = null;
List<Person> persons = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT: // 文档开始事件,可以进行数据初始化处理
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG: //开始读取某个标签
//通过getName判断读到哪个标签,然后通过nextText()获取文本节点值,或通过getAttributeValue(i)获取属性节点值
if (parser.getName().equalsIgnoreCase("person")) {
currentPerson = new Person();
currentPerson.setId(new Integer(parser.getAttributeValue(null, "id")));
}
else if (currentPerson != null) {
String name = parser.getName();
if (parser.getName().equalsIgnoreCase("name")) {
currentPerson.setName(parser.nextText());// 如果后面是Text元素,即返回它的值
} else if (name.equalsIgnoreCase("age")) {
currentPerson.setAge(new Integer(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:// 结束元素事件
if (parser.getName().equalsIgnoreCase("person") && currentPerson != null) {
persons.add(currentPerson);
currentPerson = null;
}
break;
}
eventType = parser.next();
}
inStream.close();
return persons;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// 测试用:读取文件内容
// String str = "xml文件内容:\n";
// str += this.read("person.xml");
InputStream xml = getResources().openRawResource(R.raw.persons);
String str = "xml文件内容:\n";
PersonService service = new PersonService();
List<Person> persons = service.getPersons(xml);
for(Person person :persons) str = str + person.toString() + "\n";
xml.close();
TextView textView = (TextView)findViewById(R.id.content);
textView.setText(str);
}
catch (Exception e){
e.printStackTrace();
}
}
public String read(String name) throws Exception{
InputStream is = getResources().openRawResource(R.raw.persons);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while( (len = is.read(buf)) != -1){
os.write(buf,0, len);
}
byte[] data = os.toByteArray();
String content = new String(data);
return content;
}
原文:http://blog.csdn.net/xufeng0991/article/details/44062277