安卓自带了pull 来解析xml 文件,采用面向对象编程使用起来很方便,如果在代码中自己拼接,后期在标签中加入新的属性很麻烦的所以采用面向对象方式来进行编写才是正道,我并没有研究过pull 源码,我的建议是它有一个setText 方法 如果属性是基本类型来要自己转,有兴趣的大家可以研究下
PULL解析器的运行方式和SAX类似,都是基于事件的模式。不同的是,在PULL解析过程中,我们需要自己获取产生的事件然后做相应的操作,而不像SAX那样由处理器触发一种事件的方法,执行我们的代码。PULL解析器小巧轻便,解析速度快,简单易用,非常适合在Android移动设备中使用,Android系统内部在解析各种XML时也是用PULL解析器。
关于 pull sax dom 的具体解析方式大家参考 :http://blog.csdn.net/liuhe688/article/details/6415593 写的不错
我实现了一个把数据放入sdcard 存储,进行读取的一个demo 大家可以参考一下
book 实体类
- package com.example.c;
- public class Book {
- private int id;
- private String name;
- private float price;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- @Override
- public String toString() {
- return "id:" + id + ", name:" + name + ", price:" + price;
- }
- }
具体操作activity
- package com.example.c;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- public class XmlActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_xml);
- findViewById(R.id.write).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- List<Book> books = new ArrayList<Book>();
- for(int i=1; i<=20; i++) {
- Book book = new Book();
- book.setId(i);
- book.setName("我是第" + i + "个人");
- book.setPrice(i);
- books.add(book);
- }
- XmlUtils.createXml(XmlActivity.this, books);
- }
- });
- findViewById(R.id.read).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- InputStream is;
- try {
- is = new FileInputStream(new File(Environment.getExternalStorageDirectory() + "/abc.xml"));
- List<Book> books = XmlUtils.readXml(is);
- ((TextView)findViewById(R.id.visible)).setText(books.toString());
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
- }
- package com.example.c;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- 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.XmlPullParserException;
- import org.xmlpull.v1.XmlSerializer;
- import android.content.Context;
- import android.os.Environment;
- import android.util.Log;
- import android.util.Xml;
- public class XmlUtils {
- public static void createXml(Context context,List<Book> books) {
- // 判断sdcard 是否存在
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- try {
- XmlSerializer xml = Xml.newSerializer();
- Log.d("ABC", Environment.getExternalStorageDirectory() +"") ;
- OutputStream os = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/abc.xml"));
- xml.setOutput(os,"UTF-8");
- xml.startDocument("utf-8", true);
- xml.startTag("", "books");
- for(Book book:books) {
- xml.startTag("", "book");
- xml.attribute("", "id", book.getId() + "");
- xml.startTag("", "name");
- xml.text(book.getName());
- xml.endTag("", "name");
- xml.startTag("", "price");
- xml.text(book.getPrice() + "");
- xml.endTag("", "price");
- xml.endTag("", "book");
- }
- xml.endTag("", "books");
- xml.endDocument();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //xml.
- }
- public static List<Book> readXml(InputStream is) {
- // TODO Auto-generated method stub
- List<Book> books = null;
- Book book = null;
- XmlPullParser parser = Xml.newPullParser();
- try {
- parser.setInput(is, "utf-8");
- int eventType = parser.getEventType();
- while(eventType != XmlPullParser.END_DOCUMENT) {
- switch(eventType) {
- case XmlPullParser.START_DOCUMENT:
- books = new ArrayList<Book>();
- break;
- case XmlPullParser.START_TAG:
- if(parser.getName().equals("book")) {
- book = new Book();
- } else if(parser.getName().equals("id")) {
- try {
- eventType = parser.next();
- book.setId(Integer.parseInt(parser.getText()));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } else if(parser.getName().equals("name")) {
- try {
- eventType = parser.next();
- book.setName(parser.getText());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } else if(parser.getName().equals("price")) {
- try {
- eventType = parser.next();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- book.setPrice(Float.parseFloat(parser.getText()));
- }
- break;
- case XmlPullParser.END_TAG:
- if(parser.getName().equals("book")) {
- books.add(book);
- book = null;
- }
- break;
- }
- try {
- eventType = parser.next();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return books;
- } catch (XmlPullParserException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
还有一点需要说的就是需要配置sdcard 读取权限
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
界面布局就很简单了
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/write"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="生成" />
- <Button
- android:id="@+id/read"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="读取" />
- <TextView
- android:id="@+id/visible"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
基本就可以实现读写了,需要更加复杂的自己去领悟吧