• android pull 解析器解析xml文档


    person.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persons>
        <person id="23">
            <name>liming</name>
            <age>30</age>
        </person>
        <person id="20">
            <name>zhangxiaoxiao</name>
            <age>25</age>
        </person>
    </persons>

    public class PersonService {
        /**
         * 获取数据
         * @param xml
         * @return
         * @throws Exception
         */
        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 = new Integer(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 = new Integer(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;
        }
        /**
         * 保存数据
         * @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());
                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文件路径
    getContext().getFilesDir()
  • 相关阅读:
    Eclipse配置Maven私服
    ubuntu下使用Nexus搭建Maven私服
    Ubuntu系统的Redis安装配置
    centos7.6安装dpdk-stable-19.11.10
    微软的分布式应用框架 Dapr Helloworld
    Caddy源码阅读(二)启动流程与 Event 事件通知
    Caddy源码阅读(一)Run详解
    Caddy 源码全解析
    假装前端工程师(一)Icework + GitHub pages 快速构建可自定义迭代开发的 react 网站
    caddy & grpc(3) 为 caddy 添加一个 反向代理插件
  • 原文地址:https://www.cnblogs.com/heml/p/3506297.html
Copyright © 2020-2023  润新知