• Android吧数据保存成xml文件


      1 public class MainActivity extends Activity {
      2 
      3 private List<Person> persons;
      4 @Override
      5 protected void onCreate(Bundle savedInstanceState) 
      6 {
      7 super.onCreate(savedInstanceState);
      8 setContentView(R.layout.main);
      9 //获取资源文件的输入流对象
     10 //InputStream inputstram=this.getClass().getClassLoader().getResourceAsStream("person.xml");
     11 try 
     12 {
     13 persons=new ArrayList<Person>();
     14 File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"a.xml");
     15 Person p=new Person(25, "ggg", "21", "men", "1234562");
     16 persons.add(p);
     17 FileOutputStream stram=new FileOutputStream(file);
     18 savexml(persons,stram);
     19 System.out.println("保存成功");
     20 } 
     21 catch (Exception e) {
     22 e.printStackTrace();
     23 }    
     24 }
     25 
     26 public void savexml(List<Person> person,OutputStream out)
     27 {
     28 /**
     29 * 类似于这样格式的文件
     30 * <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
     31 <persons>
     32 <person id="25">
     33 <name>ggg</name>
     34 <sex>men</sex>
     35 <age>21</age>
     36 <phone>1234562</phone>
     37 </person>
     38 </persons>
     39 */
     40 try 
     41 {
     42 //xml解析器
     43 XmlSerializer xml=Xml.newSerializer();
     44 //设置xml文件的输出方向
     45 xml.setOutput(out, "UTF-8");
     46 //设置xml的开始文档内容及编码格式
     47 xml.startDocument("UTF-8", true);
     48 //设置xml的开始节点
     49 xml.startTag(null, "persons");
     50 
     52 for(Person p:person)
     53 {
     54 xml.startTag(null, "person");
     55 
     56 String id=String.valueOf(p.getId());
     57 xml.attribute(null, "id",id);
     58 //设置开始name节点
     59 xml.startTag(null, "name");
     60 //设置name的值
     61 xml.text(p.getName());
     62 xml.endTag(null, "name");
     63 //设置name的结束节点
     64 xml.startTag(null, "sex");
     65 xml.text(p.getSex());
     66 xml.endTag(null, "sex");
     67 
     68 xml.startTag(null, "age");
     69 xml.text(p.getAge());
     70 xml.endTag(null, "age");
     71 
     72 xml.startTag(null, "phone");
     73 xml.text(p.getPhone());
     74 xml.endTag(null, "phone");
     75 //结束节点
     76 xml.endTag(null, "person");
     77 }
     78 //xml的结束节点
     79 xml.endTag(null, "persons");
     80 //结束文档
     81 xml.endDocument();
     82 } catch (Exception e) 
     83 {
     84 // TODO Auto-generated catch block
     85 e.printStackTrace();
     86 } 
     87 try
     88 {
     89 out.flush();
     90 out.close();
     91 } 
     92 catch (IOException e) 
     93 {
     94 e.printStackTrace();
     95 }
     96 
     97 }
     98 @Override
     99 public boolean onCreateOptionsMenu(Menu menu) {
    100 // Inflate the menu; this adds items to the action bar if it is present.
    101 getMenuInflater().inflate(R.menu.main, menu);
    102 return true;
    103 }
    104 
    105 }

    person类

     1 public class Person 
     2 {
     3 private int id;
     4 private String name;
     5 private String age;
     6 private String sex;
     7 private String phone;
     8 public int getId() {
     9 return id;
    10 }
    11 public void setId(int id) {
    12 this.id = id;
    13 }
    14 public String getName() {
    15 return name;
    16 }
    17 public void setName(String name) {
    18 this.name = name;
    19 }
    20 public String getAge() {
    21 return age;
    22 }
    23 public void setAge(String age) {
    24 this.age = age;
    25 }
    26 public String getSex() {
    27 return sex;
    28 }
    29 public void setSex(String sex) {
    30 this.sex = sex;
    31 }
    32 public Person(int id, String name, String age, String sex, String phone) {
    33 super();
    34 this.id = id;
    35 this.name = name;
    36 this.age = age;
    37 this.sex = sex;
    38 this.phone = phone;
    39 }
    40 public String getPhone() {
    41 return phone;
    42 }
    43 public void setPhone(String phone) {
    44 this.phone = phone;
    45 }
    46 public Person(){}
    47 }

    保存的内容结果是:

    <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
    <persons>
      <person id="25">
        <name>ggg</name>
        <sex>men</sex>
        <age>21</age>
        <phone>1234562</phone>
      </person>
    </persons>

  • 相关阅读:
    如何获取地址栏中的参数
    Ajax基础知识
    什么是BFC
    111
    不定宽 块状元素居中3
    POJ 2104主席树
    可持久化数据结构之主席树
    线段树
    Splay 学习
    hello world
  • 原文地址:https://www.cnblogs.com/tianshidechibang234/p/3187076.html
Copyright © 2020-2023  润新知