实现数据写入
org.wp.service
PULLPersonService
Java代码
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
| package org.wp.service;
import java.io.Writer;
import java.util.List;
import org.wp.domain.Person;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
public class PULLPersonService {
public static void writeXml(List<Person> persons, Writer writer)
throws Exception {
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(writer);
//
// true 代表standalone="yes"
serializer.startDocument("UTF-8", true);
// null代表没有命名空间
serializer.startTag(null, "persons");
for (Person person : persons) {
serializer.startTag(null, "person");
serializer.attribute(null, "id", String.valueOf(person.getId()));
serializer.startTag(null, "name");
serializer.text(person.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(String.valueOf(person.getAge()));
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
// flush方法即使在缓冲区中没有满的情况下
// 也会将缓冲区内的内容强制写到外设
// 也就是所谓的刷新
writer.flush();
// 关闭输入输出流
writer.close();
}
} |
org.wp.
PULLPersonServiceTest
Java代码
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
| package org.wp.xml;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.wp.domain.Person;
import org.wp.service.PULLPersonService;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
public class PULLPersonServiceTest extends AndroidTestCase {
private static final String TAG = "PULLPersonServiceTest";
public void testWriteXml() throws Exception {
FileOutputStream outStream = this.getContext().openFileOutput(
"persons.xml", Context.MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(outStream, "UTF-8");
// 缓冲写入器
BufferedWriter bufferedWriter = new BufferedWriter(writer);
// 不写入文件,直接获得XML中的内容
// StringWriter writer = new StringWriter();
List<Person> persons = new ArrayList<Person>();
persons.add(new Person(12, "张三", new Short((short) 22)));
persons.add(new Person(16, "李四", new Short((short) 36)));
persons.add(new Person(90, "田七", new Short((short) 28)));
PULLPersonService.writeXml(persons, writer);
Log.i(TAG, writer.toString());
}
} |
org.wp.domain
Person
Java代码
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
| package org.wp.domain;
public class Person {
private Integer id;
private String name;
private Short age;
public Person() {
}
public Person(Integer id, String name, Short age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
@Override
public String toString() {
return "Id=" + id + ",Name=" + name + ",Age=" + age;
}
} |
阅读(1133) | 评论(1) | 转发(0) |