Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android中XML文件的序列化生成與解析

Android中XML文件的序列化生成與解析

編輯:關於android開發

Android中XML文件的序列化生成與解析


首先,我把Person的實體類

package net.loonggg.test;  
  
public class Person {  
    private int id;  
    private String age;  
    private String name;  
    private String sex;  
    private String address;  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    public String getAge() {  
        return age;  
    }  
  
    public void setAge(String age) {  
        this.age = age;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getSex() {  
        return sex;  
    }  
  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
  
    public String getAddress() {  
        return address;  
    }  
  
    public void setAddress(String address) {  
        this.address = address;  
    }  
  
    @Override  
    public String toString() {  
        return "Person [id=" + id + ", age=" + age + ", name=" + name  
                + ", sex=" + sex + ", address=" + address + "]";  
    }  
  
}  
其次:生成xml文件的方法如下:
/** 
     * 生成xml 
     */  
    private void createXml() {  
        XmlSerializer serializer = Xml.newSerializer();// xml文件生成器  
        File file = new File(Environment.getExternalStorageDirectory(),  
                "person.xml");  
        FileOutputStream fos = null;  
        try {  
            fos = new FileOutputStream(file);  
            serializer.setOutput(fos, "utf-8");// 為xml生成器設置輸出流和字符編碼  
            serializer.startDocument("utf-8", true);// 開始文檔,參數分別為字符編碼和是否保持獨立  
            serializer.startTag(null, "persons"); // 開始標簽,參數分別為:命名空間和標簽名  
            for (Person person : list) {  
  
                serializer.startTag(null, "person");  
                serializer.attribute(null, "id", person.getId() + "");  
  
                serializer.startTag(null, "name");// 開始標簽  
                serializer.text(person.getName());// 文本內容  
                serializer.endTag(null, "name");// 結束標簽  
  
                serializer.startTag(null, "sex");  
                serializer.text(person.getSex());  
                serializer.endTag(null, "sex");  
  
                serializer.startTag(null, "age");  
                serializer.text(person.getAge());  
                serializer.endTag(null, "age");  
  
                serializer.startTag(null, "address");  
                serializer.text(person.getAddress());  
                serializer.endTag(null, "address");  
  
                serializer.endTag(null, "person");  
  
            }  
            serializer.endTag(null, "persons");// 結束標簽  
            serializer.endDocument();// 結束xml文檔  
            Toast.makeText(getApplicationContext(), "生成成功!", Toast.LENGTH_SHORT);  
        } catch (Exception e) {  
            Toast.makeText(getApplicationContext(), "生成失敗!", Toast.LENGTH_SHORT);  
            e.printStackTrace();  
        } finally {  
            try {  
                fos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
  
    }  
然後是解析xml文件的方法:
/** 
     * 解析xml文件 
     */  
    private List pullXml() {  
        try {  
            File file = new File(Environment.getExternalStorageDirectory(),  
                    "person.xml");  
            FileInputStream fis = new FileInputStream(file);  
            List persons = null;  
            Person person = null;  
            XmlPullParser parser = Xml.newPullParser();// 獲取xml解析器  
            parser.setInput(fis, "utf-8");// 參數分別為輸入流和字符編碼  
            int type = parser.getEventType();  
            while (type != XmlPullParser.END_DOCUMENT) {// 如果事件不等於文檔結束事件就繼續循環  
                switch (type) {  
                case XmlPullParser.START_TAG:  
                    if ("persons".equals(parser.getName())) {  
                        persons = new ArrayList();  
                    } else if ("person".equals(parser.getName())) {  
                        person = new Person();  
                        String id = parser.getAttributeValue(0);  
                        person.setId(Integer.parseInt(id));  
                    } else if ("name".equals(parser.getName())) {  
                        person.setName(parser.nextText());  
                    } else if ("sex".equals(parser.getName())) {  
                        person.setSex(parser.nextText());  
                    } else if ("address".equals(parser.getName())) {  
                        person.setAddress(parser.nextText());  
                    } else if ("age".equals(parser.getName())) {  
                        person.setAge(parser.nextText());  
                    }  
                    break;  
                case XmlPullParser.END_TAG:  
                    if ("person".equals(parser.getName())) {  
                        persons.add(person);  
                        person = null;  
                    }  
                    break;  
                }  
                type = parser.next();// 繼續下一個事件  
            }  
            return persons;  
        } catch (NumberFormatException e) {  
            e.printStackTrace();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (XmlPullParserException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
再次是MainActivity裡調用的部分代碼:
public class MainActivity extends Activity {  
    private Button btn_create = null;  
    private Button btn_pull = null;  
    private TextView tv_show;  
    private List list = null;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        btn_create = (Button) findViewById(R.id.createXml);  
        btn_pull = (Button) findViewById(R.id.pullXml);  
        tv_show = (TextView) findViewById(R.id.tvShow);  
        list = new ArrayList();  
        for (int i = 0; i < 5; i++) {  
            Person person = new Person();  
            person.setAge(i + 20 + "");  
            person.setId(i);  
            person.setName("loonggg" + i);  
            person.setSex("男");  
            person.setAddress("中南海" + i + 1 + "大街");  
            list.add(person);  
        }  
        btn_create.setOnClickListener(new View.OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                createXml();  
            }  
        });  
  
        btn_pull.setOnClickListener(new View.OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                tv_show.setText(pullXml().toString());  
            }  
        });  
    }  
}  

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved