import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import com.yin.xmldemo2.utils.Person;
import android.os.Environment;
import android.test.AndroidTestCase;
import android.util.Log;
import android.util.Xml;
public class XmlTest extends AndroidTestCase {
public void test(){
//写xml文件到SDcard
// writeXmlToSdCard();
List<Person> list = getXMLFromSdCard();
if(list==null){
Log.i("okong", "空");
}
Log.i("元素数", list.size()+"");
for(Person p:list){
Log.i("返回的list", p.toString());
}
}
private List<Person> getXMLFromSdCard() {
//获取文件路径
File path=new File(Environment.getExternalStorageDirectory(),"persons.xml");
//创建一个输入流
try {
FileInputStream fis=new FileInputStream(path);
//创建一个xml解析对象
XmlPullParser pull=Xml.newPullParser();
//给xml解析对象设置目标
pull.setInput(fis, "utf-8");
//获取一个事件类型
int eventType = pull.getEventType();//是哪一类标签,START_DOCUMENT、END_DOCUMENT、START_TAG、END_TAG
//解析xml
List<Person> list=null;
Person person=null;
while(eventType!=XmlPullParser.END_DOCUMENT){
String tagName = pull.getName();//解析到的当前标签的名字
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
Log.i("测试START_DOCUMENT", "测试通过");
//当前标签是START_DOCUMENT时,nextText()不可用
// Log.i("第一个startDoc", pull.nextText());
//当前标签是START_DOCUMENT时,调用next()不报错,但是一旦调用,则当前标签就是调用前的下一个标签了
// Log.i("第一个startDoc", pull.next()+"");
break;
case XmlPullParser.START_TAG:
if(tagName.equals("persons")){
Log.i("测试集合startTag", "测试通过");
//当前标签是集合START_TAG时,nextText()不可用
// Log.i("集合startTag", pull.nextText());
list=new ArrayList<Person>();
}if(tagName.equals("person")){
Log.i("测试对象startTag", "测试通过");
//当前标签是对象START_TAG时,nextText()不可用
// Log.i("对象startTag", pull.nextText());
person=new Person();
person.setId(Integer.valueOf(pull.getAttributeValue(null, "id")));
}if(tagName.equals("name")){
person.setName(pull.nextText());//解析到的当前标签的下一个文本元素的值
}if(tagName.equals("age")){
person.setAge(Integer.valueOf(pull.nextText()));
}
break;
case XmlPullParser.END_TAG:
/**
* 当当前标签是END_TAG,nextText()报错:空指针
* 当当前标签是END_DOCUMENT时,next()报错:空指针
*/
// Log.i("标签名", tagName+","+pull.nextText()+","+pull.next());
if(tagName.equals("person")){
list.add(person);
}
break;
default:
break;
}
eventType=pull.next();//移动到下一个标签的名
}
if(eventType==XmlPullParser.END_DOCUMENT){
//当前标签是END_DOCUMENT时,nextText()不可用
// Log.i("END_DOCUMENT", pull.nextText());
//当前标签是END_DOCUMENT时,next()不可用
// Log.i("END_DOCUMENT", pull.next()+"");
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void writeXmlToSdCard() {
//获取一个Person集合
List<Person> list=getPersonList();
//获取要写入的路径和文件
File file=new File(Environment.getExternalStorageDirectory(),"persons.xml");
//获取一个输出流
try {
FileOutputStream fos=new FileOutputStream(file);
//获取一个xml序列化对象
XmlSerializer serial=Xml.newSerializer();
//设置xml的目的地和编码
serial.setOutput(fos, "utf-8");
//写xml的文档开头
serial.startDocument("utf-8", true);//<?xml version="1.0" encoding="UTF-8" standalone="true"?>
//写xml的开始标签
serial.startTag(null, "persons");
//循环写集合元素到xml文件
for(Person person:list){
serial.startTag(null, "person");
serial.attribute(null, "id", String.valueOf(person.getId()));
serial.startTag(null, "name");
serial.text(person.getName());
serial.endTag(null, "name");
serial.startTag(null, "age");
serial.text(String.valueOf(person.getAge()));
serial.endTag(null, "age");
serial.endTag(null, "person");
}
//写xml的结束标签
serial.endTag(null, "persons");
//写xml的文档结尾
serial.endDocument();
} catch (Exception e) {
e.printStackTrace();
}
}
private List<Person> getPersonList() {
List<Person> list=new ArrayList<Person>();
for(int i=0;i<20;i++){
list.add(new Person(i, "jie"+i, 3+i));
}
return list;
}
}
Android中xml解析和序列化
最新推荐文章于 2022-03-18 17:55:28 发布