Sometimes, we need to generate an XML file. There are many ways to generate an XML file. For example, we can use only one stringbuilder to group XML content and then write the content into the file; you can also use the pull parser to generate xml files by using Dom APIs. We recommend that you use the pull parser.
Use the pull parser to generate a myitcast. xml file with the same content as the itcast. xml file. The Code will be noted below this page
Use the following code to generate an XML file ):
File xmlfile = new file ("myitcast. xml ");
Fileoutputstream outstream = new fileoutputstream (xmlfile );
Outputstreamwriter outstreamwriter = new outputstreamwriter (outstream, "UTF-8 ");
Bufferedwriter writer = new bufferedwriter (outstreamwriter );
Writexml (persons, writer );
Writer. Flush ();
Writer. Close ();
If you only want to get the generated XML string content, you can use stringwriter:
Stringwriter writer = new stringwriter ();
Writexml (persons, writer );
String content = writer. tostring ();
The XML file generated by the pull parser is as follows:
File Name: itcast. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> liming </Name>
<Age> 30 </age>
</Person>
<Person id = "20">
<Name> lixiangmei </Name>
<Age> 25 </age>
</Person>
</Persons>
The specific method is:
Create a person class under the package, which is mainly used to store the XML content parsed above. Javabean is a model in the MVC design model, also known as the model layer. In general programs, we call it a data layer, which is used to set data attributes and some behaviors. Then, I will provide the get/set method for getting and setting attributes.
The Code is as follows:
View code
1 package cn.itcast.domain;
2
3 public class Person {
4 private Integer id;
5 private String name;
6 private Short age;
7
8 public Person(){}
9
10 public Person(Integer id, String name, Short age) {
11 this.id = id;
12 this.name = name;
13 this.age = age;
14 }
15
16 public Integer getId() {
17 return id;
18 }
19 public void setId(Integer id) {
20 this.id = id;
21 }
22 public String getName() {
23 return name;
24 }
25 public void setName(String name) {
26 this.name = name;
27 }
28 public Short getAge() {
29 return age;
30 }
31 public void setAge(Short age) {
32 this.age = age;
33 }
34 @Override
35 public String toString() {
36 return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
37 }
38
39 }
Step 2: Create a package CN. itcast. service package and create a saxpersonservice class under the package. This is the M and service class in MVC. The main code is as follows:
View code
1 public class pullpersonservice {
2
3 Public static void save (list <person> persons, writer) throws throwable {
4 xmlserializer serializer = xml. newserializer (); // get a serialized object
5 serializer. setoutput (writer); // specify the output direction and writer of the serialized object file.
6 serializer. startdocument ("UTF-8", true); // generate the start document
7 // Start Element Node. The first parameter is the namespace name and the second parameter is the label name.
8 serializer. starttag (null, "persons ");
9 For (person: Persons ){
10 serializer. starttag (null, "person"); // Start Element Node. The first parameter is the namespace name, and the second parameter is the label name.
11 serializer. Attribute (null, "ID", person. GETID (). tostring (); // obtain the attributes in the person node.
12
13 serializer. starttag (null, "name"); // Start Element Node. The first parameter is the namespace name, and the second parameter is the label name.
14 serializer. Text (person. getname (); // get the content in the Name Node
15 serializer. endtag (null, "name"); // End Element Node. The first parameter is the namespace name, and the second parameter is the label name.
16
17 serializer. starttag (null, "Age"); // Start Element Node. The first parameter is the namespace name, and the second parameter is the label name.
18 serializer. Text (person. getage (). tostring (); // obtain the content in the age node.
19 serializer. endtag (null, "Age"); // End Element Node. The first parameter is the namespace name, and the second parameter is the label name.
20
21 serializer. endtag (null, "person"); // End Element Node. The first parameter is the namespace name, and the second parameter is the label name.
22}
23 // End Element Node. The first parameter is the namespace name and the second parameter is the label name.
24 serializer. endtag (null, "persons ");
25 serializer. enddocument (); // generate end document
26 writer. Flush ();
27 writer. Close ();
28}
29}
Step 3: Create a personservicetest class under the main package. This class is equivalent to the C (control layer) in MVC. It mainly tests the SAVE () method of the pullpersonservice class and the code.
View code
1 public class personservicetest extends androidtestcase {
2 Private Static final string tag = "personservicetest ";
3 Public void testsave () throws throwable {
4 // file = new file (this. getcontext (). getfilesdir (), "person. xml"); // store it in the current directory of the mobile phone
5 // fileoutputstream outstream = new fileoutputstream (File );
6 list <person> Persons = new arraylist <person> ();
7 persons. Add (new person (34, "Lili", (short) 12 ));
8 persons. Add (new person (56, "old Bi", (short) 32 ));
9 persons. Add (new person (39, "Lao Zhang", (short) 40 ));
10 // store it in the current directory of the mobile phone. The mode is private
11 fileoutputstream outstream = This. getcontext (). openfileoutput ("person. xml", context. mode_private );
12 outputstreamwriter writer = new outputstreamwriter (outstream, "UTF-8 ");
13 bufferedwriter bwriter = new bufferedwriter (writer );
14 pullpersonservice. Save (persons, bwriter );
15
16 // stringwriter writer = new stringwriter (); // sent to memory
17 // pullpersonservice. Save (persons, writer );
18 // log. I (TAG, writer. tostring ());
19}
20}