xml序列化

1:

package com.itheima.xmlserializer;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;

import com.itheima.xmlserializer.po.SmsInfo;

public class XmlserializerActivity extends Activity {
 private ArrayList<SmsInfo> infos = new ArrayList<SmsInfo>();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  int number = 135000;
  for (int i = 0; i < 10; i++) {
   infos.add(new SmsInfo(i, System.currentTimeMillis(), "短信" + i,
     number + i + ""));
  }
 }

 public void save(View view) {
  XmlSerializer xmlSerializer = Xml.newSerializer();
  File file = new File(Environment.getExternalStorageDirectory(),
    "config.xml");
  try {
   FileOutputStream fos = new FileOutputStream(file);
   xmlSerializer.setOutput(fos, "utf-8");
   xmlSerializer.startDocument("utf-8", true);

   xmlSerializer.startTag(null, "smss");

   for (SmsInfo info : infos) {
    xmlSerializer.startTag(null, "sms");
    xmlSerializer.attribute(null, "id", info.getId() + "");

    xmlSerializer.startTag(null, "date");
    xmlSerializer.text(info.getDate() + "");
    xmlSerializer.endTag(null, "date");

    xmlSerializer.startTag(null, "address");
    xmlSerializer.text(info.getAddress());
    xmlSerializer.endTag(null, "address");

    xmlSerializer.startTag(null, "body");
    xmlSerializer.text(info.getBody());
    xmlSerializer.endTag(null, "body");
    
    xmlSerializer.endTag(null, "sms");
   }
   xmlSerializer.endTag(null, "smss");

   xmlSerializer.endDocument();
   fos.close();
   Toast.makeText(this, "保存成功", 0).show();

  } catch (Exception e) {
   e.printStackTrace();
   Toast.makeText(this, "保存失败", 0).show();
  }

 }
}

 

2“

package com.itheima.xmlserializer.po;

public class SmsInfo {
 private int id;
 private long date;
 private String body;
 private String address;

 public SmsInfo() {
  super();
  // TODO Auto-generated constructor stub
 }

 public SmsInfo(int id, long date, String body, String address) {
  super();
  this.id = id;
  this.date = date;
  this.body = body;
  this.address = address;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public long getDate() {
  return date;
 }

 public void setDate(long date) {
  this.date = date;
 }

 public String getBody() {
  return body;
 }

 public void setBody(String body) {
  this.body = body;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

}

 

3:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
  android:onClick="save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存xml文件" />

</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值