您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

Android 生成XML文件 Demo

时间:01-11来源:作者:点击数:

Android 生成XML文件 Demo

示例点击按钮生成一个xml文件

例如XML文件格式,将数据进行封装

public class NewsInfo {
    private String title;
    private String content;
    private String date;
    public String getTitle() {
        return title;
    }
   public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    @Override
    public String toString() {
        return "NewsInfo{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", date='" + date + '\'' +
                '}';
    }
}

生成一个news.xml文件(注释很详细)

MainActivity里的代码

public class MainActivity extends AppCompatActivity {

    private List<NewsInfo> newsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }
    private void test() {
        for (NewsInfo newsInfo : this.newsList) {  /*类型是遍历数据的类型 每次遍历集合元素名字  :遍历数组或list*/
            System.out.println(newsInfo.toString());
        }
    }
    private void initData() {
        newsList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            NewsInfo newsInfo = new NewsInfo();
            newsInfo.setTitle("Title" + i);
            newsInfo.setContent("jjjjj" + i);
            /*获得日期 */
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy--MM--dd HH:mm:ss");
            String time = dateFormat.format(date);
            newsInfo.setDate(time + ";" + i);
            /*newsinfo装进来*/
            newsList.add(newsInfo);
        }
    }
  public void CreateXML(View view) {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        File file = new File(Environment.getExternalStorageDirectory().getPath(),"news.xml");
        try {
            FileOutputStream fos = new FileOutputStream(file);/*fos输出流 file路径*/
            xmlSerializer.setOutput(fos,"utf-8");/*utf_8写流编码方式*/
            xmlSerializer.startDocument("utf-8",true);/*utf_8 xml显示的编码方式,xml和别的xml有无联系*/
            xmlSerializer.startTag(null,"newss");   /*命名空间有无*/
            for (NewsInfo newsInfo:this.newsList) {
                xmlSerializer.startTag(null,"news");
                xmlSerializer.startTag(null,"title");
                xmlSerializer.text(newsInfo.getTitle());
                xmlSerializer.endTag(null,"title");
                xmlSerializer.startTag(null,"content");
                xmlSerializer.text(newsInfo.getContent());
                xmlSerializer.endTag(null,"content");
                xmlSerializer.startTag(null,"date");
                xmlSerializer.text(newsInfo.getDate());
                xmlSerializer.endTag(null,"date");
                xmlSerializer.endTag(null,"news");
            }
            xmlSerializer.endTag(null,"newss");
            xmlSerializer.endDocument();
            fos.close();
            Toast.makeText(this, "SUCCESS", Toast.LENGTH_SHORT).show();
            return;        /*方法结束标志*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        Toast.makeText(this, "FALLED", Toast.LENGTH_SHORT).show();
    }


}

点击按钮

在这里插入图片描述

最终在mnt—sdcard生成一个xml文件

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门