Android XML PULL 生成 XML

Android PULL 除了可以解析 XML 文件或字符串外,还可以用来生成 XML 文档

PULL 生成 xml 数据流程

  1. 创建 XMLSerializer 类的实例,该类用于序列化 xml

    XMLSerializer serializer  = Xml.newSerializer();
    
  2. XMLSerializer 实例设置输出流和编码格式

    serializer.setOutput(out,"UTF-8");
    
  3. XMLSerializer 实例设置输出时是否添加 XML 编码格式

    serializer.startDocument("UTF-8",true);
    
  4. 设置根元素

    serializer.startTag(null,"languages");
    
  5. 使用增强 for 循遍历对象集合,并写入标签与属性

    for (LanguageBean language : languages) {
            serializer.startTag(null, "language");
            serializer.attribute(null, "id", p.getId() + "");
            serializer.startTag(null, "name");
            serializer.text(p.getName());
            serializer.endTag(null, "name");
            serializer.startTag(null, "rank");
            serializer.text(p.getRank() + "");
            serializer.endTag(null, "rank");
            serializer.endTag(null, "language");
        }
    
  6. 设置结束根元素

    serializer.endTag(null,"languages");
    
  7. 结束文档编写

    serializer.endDocument();
    
  8. 调用输出流的 flush() 方法将内存中的数据写入文件,并且关闭输出流

    out.flush();
    out.close();
    

范例

  1. 创建一个 空的 Android 项目 cn.twle.android.PULLGenXml

  2. 修改 activity_main.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:orientation="vertical" >
    
        <Button
            android:text="生成"
            android:id="@+id/btn_gen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/ms_dst"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="" />
    </LinearLayout>
    
  3. 创建一个 POJO 业务类 Language.java

    package cn.twle.android.pullgenxml;
    
    public class Language {
        private int id;
        private String name;
        private String rank;
    
        public Language() {
        }
    
        public Language(int id,String name,String rank){
    
            this.id = id;
            this.name = name;
            this.rank = rank;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getId() {
            return this.id;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setRank(String rank) {
            this.rank = rank;
        }
    
        public String getRank() {
            return this.rank;
        }
    }
    
  4. 修改 MainActivity.java

    package cn.twle.android.pullgenxml;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Xml;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import org.xmlpull.v1.XmlSerializer;
    
    import java.io.ByteArrayOutputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private TextView ms_dst;
    
        private Button btn_gen;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ms_dst = findViewById(R.id.ms_dst);
    
            btn_gen = findViewById(R.id.btn_gen);
            btn_gen.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    List<Language> langs = new ArrayList<Language>();
    
                    langs.add(new Language(1, "Java", "1"));
                    langs.add(new Language(2, "C", "2"));
                    langs.add(new Language(3, "C++", "3"));
    
                    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    
                    try {
                        ;
                        save(langs, bs);
    
                        ms_dst.setText(new String(bs.toByteArray()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
    
        }
    
        public static void save(List<Language> persons, OutputStream out) throws Exception {
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(out, "UTF-8");
            serializer.startDocument("UTF-8", true);
            serializer.startTag(null, "languages");
            for (Language p : persons) {
                serializer.startTag(null, "language");
                serializer.attribute(null, "id", p.getId() + "");
                serializer.startTag(null, "name");
                serializer.text(p.getName());
                serializer.endTag(null, "name");
                serializer.startTag(null, "rank");
                serializer.text(p.getRank() + "");
                serializer.endTag(null, "rank");
                serializer.endTag(null, "language");
            }
    
            serializer.endTag(null, "languages");
            serializer.endDocument();
            out.flush();
            out.close();
        }
    }
    

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.