使用pull方式解析xml文件示例:

网上的示例太多,基本类似,个人在此做个简单的总结:

1、首先在工程的asserts目录下建一个book.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<name>thinking in java</name>
<price>85.5</price>
</book>
<book id="2">
<name>Spring in Action</name>
<price>39.0</price>
</book>
</books>

2、创建Book.java类

/**
*
*/
package com.hyzhou.pulldemo; /**
* @author hyzhou
*
* 2013-10-17
*/
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 this.id + ":" + this.name + ":" + this.price;
} }

3、创建PullParse.java类

/**
*
*/
package com.hyzhou.pulldemo; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.util.Xml;
/**
* @author hyzhou
*
* 2013-10-17
*/
public class PullParse { public static List<Book> getBooks(InputStream inputStream) throws Exception
{
XmlPullParser parser=Xml.newPullParser();
List<Book> books=null;
Book book=null;
parser.setInput(inputStream, "UTF-8");//以流的形式将xml文件载入 int event=parser.getEventType();//产生第一个事件 while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT://判断当前事件是否是文档开始事件
books=new ArrayList<Book>();//初始化books集合
break;
case XmlPullParser.START_TAG: //判断当前事件是否是标签元素开始事件
if ("book".equals(parser.getName())) {//判断开始标签元素是否是book
book = new Book();
book.setId(Integer.parseInt(parser.getAttributeValue(0)));//得到book标签的属性值,并设置book的id }
else if ("name".equals(parser.getName())) {
book.setName(parser.nextText());
}
else if ("price".equals(parser.getName())) {
book.setPrice(Float.parseFloat(parser.nextText()));
}
/**
if (book!=null) { if("name".equals(parser.getName())) //判断开始标签元素是否是name
{ //判断开始标签元素是否是name
book.setName(parser.nextText());
}else if("price".equals(parser.getName())){//判断开始标签元素是否是price
book.setPrice(Float.parseFloat(parser.nextText()));
}
}
**/
break;
case XmlPullParser.END_TAG:
if("book".equals(parser.getName())){//判断结束标签元素是否是book
books.add(book);//将book添加到books集合
book = null;
}
break;
} event=parser.next();//进入下一个元素并触发相应事件
}
return books;
}
}

4、编写MainActivity.java类

package com.hyzhou.pulldemo;

import java.io.InputStream;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private TextView tView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tView = (TextView) findViewById(R.id.tv);
try {
//用类加载器加载xml文件,book.xml放在了assets文件下
InputStream inputStream=getResources().getAssets().open("book.xml");
List<Book> books = PullParse.getBooks(inputStream); StringBuffer sb = new StringBuffer(); for (Book book : books) {
String str = book.toString();
sb.append(str);
sb.append("\n");
}
tView.setText(sb.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} }

源码下载地址

上一篇:SAP BO WebI 如何连接webi server folder下面的EXCEL文件作为数据源


下一篇:c#获取指定时区的日期