前言
XmlSerializer是Android提供的一个类,用于创建和编辑XML文档。
本文简单记录一下XmlSerializer的使用。
正文
常用方法
下面是XmlSerializer创建xml文档的常用的方法。
1. endDocument() 结束XML文档 2. startDocument (String encoding, Boolean standalone) 开始xml文档 3. startTag(String namespace, String tagName) 开始一个标签。 4. endTag(String namespace, String tagName) 结束一个标签。 5. attribute(String namespace, String name, String value) 为标签添加一个属性。 6. text(String text) 添加文本内容
简单示例
直接上代码。
public class Student { private int age; private String name; private float score; public Student(int age, String name, float score) { this.age = age; this.name = name; this.score = score; } public String getName() { return name; } public int getAge() { return age; } public float getScore() { return score; } } //初始化数据 private ArrayList<Student> mList = new ArrayList<>(); mList.add(new Student(16, "张三", 500)); mList.add(new Student(17, "李四", 490)); mList.add(new Student(15, "王五", 580)); //xml路径 mXmlPath = getFilesDir().getPath() + "/student.xml";
例子1
FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(mXmlPath); XmlSerializer xmlSerializer = Xml.newSerializer(); xmlSerializer.setOutput(fileOutputStream, "UTF-8"); xmlSerializer.startDocument(null, true); xmlSerializer.text("\n"); xmlSerializer.startTag(null, "school"); xmlSerializer.text("\n"); for (Student student : mList) { xmlSerializer.startTag(null, "student"); xmlSerializer.attribute(null, "age", Integer.toString(student.getAge())); xmlSerializer.attribute(null, "name", student.getName()); xmlSerializer.attribute(null, "score", Float.toString(student.getScore())); xmlSerializer.endTag(null, "student"); xmlSerializer.text("\n"); } xmlSerializer.endTag(null, "school"); xmlSerializer.endDocument(); //xmlSerializer.flush(); //fileOutputStream.getFD().sync(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
注意,上面使用了换行符。这里只是方便观看。
xmlSerializer.text("\n");
最后,student.xml中内容:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <school> <student age="16" name="张三" score="500.0" /> <student age="17" name="李四" score="490.0" /> <student age="15" name="王五" score="580.0" /> </school>
例子2
这使用了Gson,主要是要存储自定义类的List,后面还需要进行解析。
implementation 'com.google.code.gson:gson:2.11.0'
Gson mGson = new GsonBuilder().setPrettyPrinting().create();
FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(mXmlPath); XmlSerializer xmlSerializer = Xml.newSerializer(); xmlSerializer.setOutput(fileOutputStream, "UTF-8"); xmlSerializer.startDocument(null, true); xmlSerializer.text("\n"); xmlSerializer.startTag(null, "school"); xmlSerializer.text("\n"); xmlSerializer.startTag(null, "mList"); xmlSerializer.attribute(null, "data", mGson.toJson(mList)); xmlSerializer.endTag(null, "mList"); xmlSerializer.text("\n"); xmlSerializer.endTag(null, "school"); xmlSerializer.endDocument(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
直接把mList进行写入xml,最后,student.xml中内容:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <school> <mList data='[ { "age": 16, "name": "张三", "score": 500.0 }, { "age": 17, "name": "李四", "score": 490.0 }, { "age": 15, "name": "王五", "score": 580.0 } ]' /> </school>
参考文章
© 版权声明