-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateXml
77 lines (56 loc) · 1.94 KB
/
CreateXml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.etcxy.prats.delete_pullparser;
import android.util.Xml;
import org.xmlpull.v1.XmlSerializer;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by etcxy@live.cn on 2017/8/27.
*/
public class demo {
private List<Message> smsList;
public void xmlSeriliaze() {
smsList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Message sms = new Message("我" + i, "mimashi:" + i, i % 2 == 0 ? "xue" : "shehui", "Changjiang" + i + "hao");
smsList.add(sms);
}
useXmlSerializer();
}
private void useXmlSerializer() {
//使用xml序列化生成器
//1.拿到序列化器对象
XmlSerializer xs = Xml.newSerializer();
//2.初始化
File file = new File("data/data/com.etcxy.prats.delete_pullparser/sms.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
xs.setOutput(fos, "utf-8");
//3.开始生成xml文件
xs.startDocument("utf-8", true);
xs.startTag(null, "message");
for (Message sms : smsList) {
xs.startTag(null, "sms");
xs.startTag(null, "name");
xs.text(sms.getName());
xs.endTag(null, "name");
xs.startTag(null, "key");
xs.text(sms.getKey());
xs.endTag(null, "key");
xs.startTag(null, "identity");
xs.text(sms.getIdentity());
xs.endTag(null, "identity");
xs.startTag(null, "address");
xs.text(sms.getAddress());
xs.endTag(null, "address");
xs.endTag(null, "sms");
}
xs.endTag(null, "message");
xs.endDocument();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}