实例教程四:采用Pull解析器解析和生成XML内容

前端之家收集整理的这篇文章主要介绍了实例教程四:采用Pull解析器解析和生成XML内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Android——activity生命周期Demo
http://www.eoeandroid.com/thread-207556-1-1.html

Android——service生命周期Demo
http://www.eoeandroid.com/thread-207558-1-1.html

android spinner 基础小实例
http://www.eoeandroid.com/thread-207188-1-1.html

<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:layout_width="fill_parent"
 
    android:layout_height="fill_parent"
 
    android:orientation="vertical" >
 

    <TextView
 
        android:layout_width="fill_parent"
 
        android:layout_height="wrap_content"
 
        android:text="@string/hello" />
 

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
 
<resources>
 

    <string name="hello">Hello World,MainActivity!</string>
 
    <string name="app_name">XML解析</string>
 

</resources>

在src下新建的Person.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<persons>
 
    <person id="23">
 
        <name>LiMing</name>
 
        <age>30</age>
 
    </person>
 
    <person id="20">
 
        <name>ZhangSan</name>
 
        <age>25</age>
 
    </person>
 
</persons>
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;
 
        }
 
        public void setName(String name) {
 
                this.name = name;
 
        }
 
        public Integer getAge() {
 
                return age;
 
        }
 
        public void setAge(Integer age) {
 
                this.age = age;
 
        }
 
        
        @Override
 
        public String toString() {
 
                return "Person [id=" + id + ",name=" + name + ",age=" + age + "]";
 
        }
 
        
        public Person(){
 
                
        }
 
        
        public Person(Integer id,String name,Integer age) {
 
                super();
 
                this.id = id;
 
                this.name = name;
 
                this.age = age;
 
        }                
}
 
package cn.itcast.xml;
 

import android.app.Activity;
 
import android.os.Bundle;
 

public class MainActivity extends Activity {
 
    /** Called when the activity is first created. */
 
    @Override
 
    public void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
 
    }
 
}
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.XmlPullParserFactory;
 
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> getPerson(InputStream xml) throws Exception{
 
                List<Person> persons = null;
 
                Person person = null;
 
                
                //第一种写法
 
//                XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
 
                //第二种写法
 
                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);
 
                                }
 
                                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); //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();
 
        }
 
}
 

单元测试:

package cn.itcast.test;
 

import java.io.File;
 
import java.io.FileOutputStream;
 
import java.io.InputStream;
 
import java.util.ArrayList;
 
import java.util.Iterator;
 
import java.util.List;
 

import cn.itcast.domain.Person;
 
import cn.itcast.service.PersonService;
 
import android.test.AndroidTestCase;
 
import android.util.Log;
 

public class PersonServiceTest extends AndroidTestCase{
 
        private static final String TAG = "PersonServiceTest";
 
        
        public void testPersons() throws Exception{
 
                InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");
 
                List<Person> persons = PersonService.getPerson(xml);
 
                for(Person person : persons){
 
                        Log.i(TAG,person.toString());
 
                }
 
        }
 
        
        public void testSave() throws Exception{
 
                List<Person> persons = new ArrayList<Person>();
 
                persons.add(new Person(1,"zhangsan",80));
 
                persons.add(new Person(2,"lisi",43));
 
                persons.add(new Person(3,"wangwu",12));
 
                //<包> ///file
 
                File xmlFile = new File(getContext().getFilesDir(),"itcast.xml");
 
                FileOutputStream outStream = new FileOutputStream(xmlFile);
 
                
                PersonService.save(persons,outStream);
 
        }
 
}
 

猜你在找的XML相关文章