打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用Pull方法生成一个XML?并解析
使用Pull方法生成一个XML 并解析
2011年9月26日15:58
来源:Android中文网
我有话说(0)
1.首先在SD卡上新建一个测试文件夹,用来保存生成的XML
// 获取保存路径
public File getFilePath() {
File filePath = null;
//判断SD卡存在与否
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+
"/A_Test/test/");
if (!filePath.isDirectory()) {//判断文件存在与否,不存在就创建
filePath.mkdirs();
}
} else {
Toast.makeText(Main.this, "存储卡不存在,请插入卡!", 3000).show();
}
return filePath;
}
注意:权限的添加:
<!-- SD卡写入 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
2.生成一个测试的XML文件
File file = new File(getFilePath() + "/test.xml");
if (!file.exists()) {// 如果文件不存在,就创建新文件
System.out.println("文件不存在,马上创建!");
List<PersonModel> personModel = new ArrayList<PersonModel>();
personModel.add(new PersonModel(1001, "张三", (short) 30));
personModel.add(new PersonModel(1002, "李四", (short) 18));
personModel.add(new PersonModel(1003, "王二", (short) 21));
CreateXML.createXML(file, personModel);
}
/**
* 生成XML文件
*
* @author lilin
* @date 2011-9-26 上午08:36:08
* @ClassName: CreateXML
* @Description: TODO
*/
public class CreateXML {
// 生成XML文件
public static void createXML(File file, List<PersonModel> personModel)
throws IllegalArgumentException, IllegalStateException, IOException {
FileOutputStream outputStream = null;
OutputStreamWriter writer = null;
outputStream = new FileOutputStream(file);
writer = new OutputStreamWriter(outputStream, "UTF-8");
BufferedWriter writer2 = new BufferedWriter(writer);// 增加的缓存功能
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(writer2);
serializer.setOutput(outputStream, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "persons");
for (PersonModel persons : personModel) {
serializer.startTag(null, "person");
serializer.attribute(null, "id", persons.getId().toString());
serializer.startTag(null, "name");
serializer.text(persons.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(persons.getAge().toString());
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
outputStream.close();
}
}
3.解析XML
FileInputStream fileInputStream = new FileInputStream(file);
List<PersonModel> personModel = PullXML.pullXML(fileInputStream);
for (int i = 0; i < personModel.size(); i++) {
System.out.println(personModel.get(i).getId() + "--"
+ personModel.get(i).getName() + "--"
+ personModel.get(i).getAge());
}
/**
* 解析XML
*
* @author lilin
* @date 2011-9-26 上午09:23:37
* @ClassName: PullXML
* @Description: TODO
*/
public class PullXML {
public static List<PersonModel> pullXML(InputStream inStream)
throws Throwable {
List<PersonModel> persons = null;
PersonModel person = null;
/* 声明XML的pull解析器 */
XmlPullParser parser = Xml.newPullParser();
/* 声明编码方式 */
parser.setInput(inStream, "UTF-8");
int eventType = parser.getEventType();// 产生第一个事件
while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
switch (eventType) {
case XmlPullParser.START_DOCUMENT:// 0 文档开始事件
persons = new ArrayList<PersonModel>();
break;
case XmlPullParser.START_TAG:// 2 开始元素
String name = parser.getName();// 获取解析器当前指向的元素的名称
if ("person".equals(name)) {
person = new PersonModel();
person.setId(new Integer(parser.getAttributeValue(0)));
}
if (person != null) {
if ("name".equals(name)) {
person.setName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
}
if ("age".equals(name)) {
person.setAge(new Short(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if ("person".equals(parser.getName())) {
persons.add(person);
person = null;
}
break;
}
eventType = parser.next();
}
return persons;
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android笔试总结
Android解析XML文件的三方法
Android XML解析--中国移动开发者社区
XML解析之
7.2.1 Android XML数据解析
Android使用Pull解析器解析XML文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服