Android - XML序列化

 

1、xml文件的序列化:采用XmlSerializer来实现XML文件的序列化。相比传统方式,更高效安全

 

MainActivity.java

 1 package com.example.test_buildxmlfile;

 2 

 3 import java.io.File;

 4 import java.io.FileOutputStream;

 5 import java.util.ArrayList;

 6 import java.util.List;

 7 import java.util.Random;

 8 

 9 import org.xmlpull.v1.XmlSerializer;

10 

11 import android.app.Activity;

12 import android.os.Bundle;

13 import android.os.Environment;

14 import android.util.Log;

15 import android.util.Xml;

16 import android.view.View;

17 import android.widget.Toast;

18 

19 import com.example.test_getxmlfile.R;

20 import com.test.MsgInfo.MsgInfo;

21 

22 /**

23  * @author daomul

24  *  主入口:xml文件的序列化

25  */

26 public class MainActivity extends Activity {

27 

28     private List<MsgInfo> msgList;

29     @Override

30     protected void onCreate(Bundle savedInstanceState) {

31         super.onCreate(savedInstanceState);

32         setContentView(R.layout.activity_main);

33         

34         msgList = new ArrayList<MsgInfo>();

35         Random random= new Random(); //随机的 (1:发送      2 :接收)

36         long number = 1340000001;    //手机地址

37         

38         //连续生成10条默认短信

39         for (int i = 0; i < 10; i++) {

40             msgList.add(new MsgInfo(System.currentTimeMillis(), random.nextInt(2), 

41                     "text MSg"+i,Long.toString(number+i),i ));

42         }

43     }

44  

45     /**

46      * 短信信息备份

47      * @param view 点击事件触发

48      */

49     public void MsgSave(View view){

50         

51         try {

52             //XML序列化

53             XmlSerializer serializer= Xml.newSerializer();

54 

55             File file=new File(Environment.getExternalStorageDirectory(),"test.xml");

56             FileOutputStream os= new FileOutputStream(file);

57             

58             serializer.setOutput(os, "utf-8");//os 是导出的文件位置  encoding 指代utf-8

59             serializer.startDocument("utf-8", true);

60 

61             serializer.startTag(null, "msgs");

62             for (MsgInfo msg:msgList) {

63                 serializer.startTag(null, "msg");

64                 serializer.attribute(null, "msg", msg.getId()+"");//添加ID属性

65                 

66                 serializer.startTag(null, "body");

67                 serializer.text(msg.getBody());

68                 serializer.endTag(null, "body");

69 

70                 serializer.startTag(null, "address");

71                 serializer.text(msg.getAddress());

72                 serializer.endTag(null, "address");

73 

74                 serializer.startTag(null, "type");

75                 serializer.text(msg.getType()+"");

76                 serializer.endTag(null, "type");

77                 

78                 serializer.startTag(null, "date");

79                 serializer.text(msg.getDate()+"");

80                 serializer.endTag(null, "date");

81                 

82                 serializer.endTag(null, "msg");

83             }

84             serializer.endTag(null, "msgs");

85             

86             serializer.endDocument();

87             

88             os.close();

89             Toast.makeText(this, "XML序列化成功", 0).show();

90             

91         } catch (Exception e) {

92             // TODO Auto-generated catch block

93             e.printStackTrace();

94             Toast.makeText(this, "XML序列化失败", 0).show();

95         } 

96     }

97 }

    

MsgInfo.java

 1 package com.test.MsgInfo;

 2 

 3 /**

 4  * @author daomul

 5  * 短信信息

 6  */

 7 

 8 public class MsgInfo {

 9     

10     private long date;

11     private int type;

12     private String body;

13     private String address;

14     private int id;

15     

16     //无参构造方法

17     public MsgInfo() {

18         

19     }

20     

21     //有参构造方法 Generate Constructor use fields...

22     public MsgInfo(long date, int type, String body, String address,int id) {

23         super();

24         this.date = date;

25         this.type = type;

26         this.body = body;

27         this.address = address;

28         this.id = id;

29     }

30 

31     //setter and getter

32     public int getId() {

33         return id;

34     }

35 

36     public void setId(int id) {

37         this.id = id;

38     }

39 

40     public long getDate() {

41         return date;

42     }

43     public void setDate(long date) {

44         this.date = date;

45     }

46     public int getType() {

47         return type;

48     }

49     public void setType(int type) {

50         this.type = type;

51     }

52     public String getBody() {

53         return body;

54     }

55     public void setBody(String body) {

56         this.body = body;

57     }

58     public String getAddress() {

59         return address;

60     }

61     public void setAddress(String address) {

62         this.address = address;

63     }

64     

65     

66 

67 }


注: 写文件到SD卡中需要 user 权限

你可能感兴趣的:(android)