• android最最基础简单的保存xml代码


    y偶遇非常基础,我想直接上代码:

    MainActivity.java:

    package com.lgqchinese.xmlcreate;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Xml;
    import android.view.View;
    
    import org.xmlpull.v1.XmlSerializer;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void myClick(View view) {
    
            try {
                XmlSerializer serializer = Xml.newSerializer();
                File file = new File("/data/data/com.lgqchinese.xmlcreate/text.xml");
                FileOutputStream fos = new FileOutputStream(file);
                serializer.setOutput(fos, "utf-8");
                serializer.startDocument("utf-8", true);
                serializer.startTag(null, "smss");
                serializer.startTag(null, "sms");
                serializer.startTag(null, "text");
                serializer.text("text内容1");
                serializer.endTag(null, "text");
                serializer.startTag(null, "text");
                serializer.text("text内容2");
                serializer.endTag(null, "text");
                serializer.startTag(null, "text");
                serializer.text("text内容3");
                serializer.endTag(null, "text");
                serializer.endTag(null, "sms");
                serializer.endTag(null, "smss");
                serializer.endDocument();
                fos.close();
                System.out.println("保存成功");
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("未找到文件");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("输入流异常");
            }
    
        }
    }

    结果为:

    <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
    <smss>
        <sms>
            <text>text内容1</text>
            <text>text内容2</text>
            <text>text内容3</text>
        </sms>
    </smss>

     

    昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
  • 相关阅读:
    springboot对JPA的支持
    springboot整合redis
    spring boot整合mybatis
    mybatis与spring集成
    mybatis动态sql和分页
    mybatis入门
    使用java代码操作redis
    Redis安装和基本操作
    idea安装及使用
    爬虫
  • 原文地址:https://www.cnblogs.com/lgqrlchinese/p/9885067.html
Copyright © 2020-2023  润新知