zoukankan      html  css  js  c++  java
  • 15_采用Pull解析器解析和生成XML内容

    java还提供SAX和DOM用于解析XML

    Android还集成了Pull解析器——推荐

    package cn.itcast.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 android.util.Xml;
    
    import cn.itcast.domain.Person;
    
    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");
            int event = pullparser.getEventType();
            
            while (event!=pullparser.END_DOCUMENT){
                switch (event) {
                case XmlPullParser.START_DOCUMENT:
                    //    数据初始化
                    persons = new ArrayList<Person>();
                    break;
                case XmlPullParser.START_TAG:
                    //    数据read
                    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);
                    }
                    break;
                default:
                    break;
                }    
                event = pullparser.next();
            }
            return persons;
        }
        
        /**
         * 保存数据
         * @param persons
         * @param out
         * @throws Exception
         */
        public static void savePersons(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();
            
        }
    }

    //  单元测试

        <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:targetPackage="cn.itcast.xml" />
        
        <application
             <uses-library android:name="android.test.runner" />
    public class PersonServiceTest extends AndroidTestCase {
        
        public void testPersons() throws Exception{
            InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");
            List<Person> persons = PersonService.getPersons(xml);
            for (Person person:persons){
                Log.i("test111", person.toString());
            }
        }
        
        public void testSave() throws Exception{
            List<Person> persons = new ArrayList<Person>();
            persons.add(new Person(10, "10", 10));
            persons.add(new Person(20, "20", 20));
            
            File xml = new File(getContext().getFilesDir(), "itcast.xml");
            FileOutputStream outputStream = new FileOutputStream(xml); 
            PersonService.savePersons(persons, outputStream);
            outputStream.close();
        }
    }


    //  数据结构

    package cn.itcast.domain;
    
    public class Person {
        private Integer id;
        private String name;
        private Integer age;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        @Override
        public String toString() {
            return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Person(Integer id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
        
        public Person() {}
        
        
    }
  • 相关阅读:
    十分钟内学会:控制浏览器是否缓存网页状态
    编写 iPhone Friendly 的 Web 应用程序 (Part 7 多点触击)
    写个 JavaScript 异步调用框架 (Part 4 链式调用)
    拆分自然数:纯while实现 (Part 2 实现)
    诚聘 项目经理 & C++开发高手
    李天平:日常管理随笔一
    诚聘:.Net 软件工程师
    对 Enterprise Library 2.0 进一步封装: DbHelperSQL2
    李天平:无为而治&灯下黑
    累并快乐着!
  • 原文地址:https://www.cnblogs.com/carl2380/p/4159619.html
Copyright © 2011-2022 走看看