zoukankan      html  css  js  c++  java
  • 【转】Android中xml文件读写

    Android XML文件读写

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <persons>
        <person id="23">
            <name>eboy</name>
            <age>22</age>
        </person>
        <person id="24">
            <name>Frr</name>
            <age>20</age>
        </person>
    </persons>
    复制代码

     package com.eboy.service;

    复制代码

    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlSerializer;

    import com.eboy.domain.Person;

    import android.util.Xml;

    public class PersonService {
        public static List<Person> getPersons(InputStream xml) throws Exception {        
            List<Person> persons = null;
            Person person = null;
            XmlPullParser pullParser = Xml.newPullParser();
            pullParser.setInput(xml, "UTF-8"); //为Pull解释器设置要解析的XML数据        
            int event = pullParser.getEventType();
            
            while (event != XmlPullParser.END_DOCUMENT){
                
                switch (event) {
                
                case XmlPullParser.START_DOCUMENT:
                    persons = new ArrayList<Person>();                
                    break;    
                case XmlPullParser.START_TAG:    
                    if ("person".equals(pullParser.getName())){
                        int id = Integer.valueOf(pullParser.getAttributeValue(0));
                        person = new Person();
                        person.setId(id);
                    }
                    if ("name".equals(pullParser.getName())){
                        String name = pullParser.nextText();
                        person.setName(name);
                    }                                
                    if ("age".equals(pullParser.getName())){
                        int age = Integer.valueOf(pullParser.nextText());
                        person.setAge(age);
                    }
                    break;
                    
                case XmlPullParser.END_TAG:
                    if ("person".equals(pullParser.getName())){
                        persons.add(person);
                        person = null;
                    }
                    break;
                    
                }
                
                event = pullParser.next();
            }
            
            
            return persons;
        }
        
        /**
         * 保存数据到xml文件中
         * 
    @param persons
         * 
    @param out
         * 
    @throws Exception
         
    */
        public static void save(List<Person> persons, OutputStream out) throws Exception {
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(out, "UTF-8");
            serializer.startDocument("UTF-8", true);
            serializer.startTag(null, "persons");        
            for (Person person : persons) {
                serializer.startTag(null, "person");            
                serializer.attribute(null, "id", person.getId().toString());            
                serializer.startTag(null, "name");            
                serializer.text(person.getName().toString());
                serializer.endTag(null, "name");            
                serializer.startTag(null, "age");            
                serializer.text(person.getAge().toString());
                serializer.endTag(null, "age");            
                serializer.endTag(null, "person");
            }        
            serializer.endTag(null, "persons");
            serializer.endDocument();
            out.flush();
            out.close();
        }
    }
    复制代码

    /Files/jxgxy/com.eboy.readwritexml.MainActivity.rar

    转载地址

    http://www.cnblogs.com/jxgxy/archive/2012/07/26/2609730.html
    有些东西会,但不精通,而我现在要做的就是精通,复习回顾,整理自己的框架,举一反三。。。
  • 相关阅读:
    【整理】PHP获取客户端真实IP地址详解
    配置百度编辑器变成纯代码编辑器
    Notepad++安装SVN插件
    【CodeBase】【转】php随机生成汉字
    【CodeBase】PHP打印所有用户自定义常量
    php5.3新垃圾回收机制详解
    php脚本cli 模式运行
    php 内存分配新
    php-fpm epoll封装
    火焰图定位dbproxy问题
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2932512.html
Copyright © 2011-2022 走看看