首页 > 代码库 > Android基础--XML序列化(XmlSerializer)

Android基础--XML序列化(XmlSerializer)

1.封装节点的JavaBean

public class City {    private String name;    private String temp;    private String pm25;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getTemp() {        return temp;    }    public void setTemp(String temp) {        this.temp = temp;    }    public String getPm25() {        return pm25;    }    public void setPm25(String pm25) {        this.pm25 = pm25;    }    @Override    public String toString() {        return "City [name=" + name + ", temp=" + temp + ", pm25=" + pm25 + "]";    }    public City(String name, String temp, String pm25) {        super();        this.name = name;        this.temp = temp;        this.pm25 = pm25;    }    public City() {        super();    }    }

 

2.生成xml的MainActivity.java ,其中createXmlFile是绑定在一个Button控件上的点击事件

package com.itheima.xmlserializer;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;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 com.itheima.xmlserializer.domain.City;public class MainActivity extends Activity {    List<City> cityList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        cityList = new ArrayList<City>();        City city = null;        for (int i = 0; i < 5; i++) {            city = new City("城市" + i, 20 + i + "", 100 + i + "");            cityList.add(city);        }    }    public void createXmlFile(View v) {        // 判断当前SD卡是否可用        if (Environment.getExternalStorageState().equals(                Environment.MEDIA_MOUNTED)) {            // 指定存放xml文件的位置            File file = new File(Environment.getExternalStorageDirectory(),                    "info.xml");            // 获得XML序列化器            XmlSerializer xs = Xml.newSerializer();            OutputStream os = null;            try {                //OutputStream :指定文件的保存路径                //encoding:指定生成的xml文件的编码                os = new FileOutputStream(file);                xs.setOutput(os, "UTF-8");                // 开始生成文件                // 设置xml文件第一行<?xml encoding = ? standalone=?>                xs.startDocument("UTF-8", true);                //生成根节点开始标签                xs.startTag(null, "weather");                for (City city : cityList) {                    xs.startTag(null, "city");                                        xs.startTag(null, "name");                    //生成文本节点                    xs.text(city.getName());                    xs.endTag(null, "name");                                        xs.startTag(null, "temp");                    //生成文本节点                    xs.text(city.getTemp());                    xs.endTag(null, "temp");                                        xs.startTag(null, "pm25");                    //生成文本节点                    xs.text(city.getPm25());                    xs.endTag(null, "pm25");                                        xs.endTag(null, "city");                }                //生成根节点结束标签                xs.endTag(null, "weather");                xs.endDocument();            } catch (Exception e) {                throw new RuntimeException(e);            }        }    }}

 

3.生成的XML文件如下,因为没有换行,所有可读性差一些

<?xml version=‘1.0‘ encoding=‘UTF-8‘ standalone=‘yes‘ ?><weather><city><name>城市0</name><temp>20</temp><pm25>100</pm25></city><city><name>城市1</name><temp>21</temp><pm25>101</pm25></city><city><name>城市2</name><temp>22</temp><pm25>102</pm25></city><city><name>城市3</name><temp>23</temp><pm25>103</pm25></city><city><name>城市4</name><temp>24</temp><pm25>104</pm25></city></weather>

 

Android基础--XML序列化(XmlSerializer)