android pull解析xml文件实例

要解析的xml文件 放置在 assets文件夹中: index.xml

<?xml version="1.0" encoding="utf-8"?>
<books>
	<book>
		<id>1001</id>
		<name>Thinking In Java</name>
		<price>80.00</price>
	</book>
	<book>
		<id>1002</id>
		<name>Core Java</name>
		<price>90.00</price>
	</book>
	<book>
		<id>1003</id>
		<name>Hello, Andriod</name>
		<price>100.00</price>
	</book>
</books>  

解析xml 对象类:

package com.example.mytool;

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;
	}
	@Override
	public String toString() {
		return "book [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
}



业务逻辑代码:


package com.example.mytool;

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

import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.util.Xml;

public class Filecaozuo {
	private Context context;
	
	public Filecaozuo(Context context) {
		this.context = context;
	}
	
	public void save_sdcard(String fname,String fcontent) throws Exception{
		File mf=new File(android.os.Environment.getExternalStorageDirectory(),fname);
		FileOutputStream outfile=new FileOutputStream(mf);
		outfile.write(fcontent.getBytes());
		outfile.close();
	}

	public void save(String mfname, String mneirong) throws Exception {
		// TODO Auto-generated method stub
		FileOutputStream xieru=context.openFileOutput(mfname, context.MODE_PRIVATE);
		xieru.write(mneirong.getBytes());
		xieru.close();
	}
	
	public String read(String fname) throws Exception{
		InputStream infile=context.getAssets().open(fname);
		int len=infile.available();
		byte[] str=new byte[len];
		infile.read(str);
		infile.close();
		return new String(str,"utf-8");
	} 
	/*
	 * pull解析xml逻辑
	 * */
	public List<book> read_xml(InputStream instr) throws Exception{
		List<book> books=null;
		book book=null;
		XmlPullParser parser=Xml.newPullParser();
		parser.setInput(instr, "utf-8");
		int event=parser.getEventType();
		while(event!=parser.END_DOCUMENT){
			switch(event){
			case XmlPullParser.START_DOCUMENT:
				books=new ArrayList<book>();
				break;
			case XmlPullParser.START_TAG:
				if("book".equals(parser.getName())){
					book=new book();
				}
				if("id".equals(parser.getName())){
					book.setId(Integer.parseInt(parser.nextText()));
				}
				if("name".equals(parser.getName())){
					book.setName(parser.nextText());
				}
				if("price".equals(parser.getName())){
					book.setPrice(Float.parseFloat(parser.nextText()));
				}
				break;
			case XmlPullParser.END_TAG:
				if("book".equals(parser.getName())){
					books.add(book);
					book=null;
				}
				break;
			}
			event=parser.next();
		}
		return books;
	}
	
	
	
}



activity调用方法:

		Filecaozuo mfile=new Filecaozuo(this.getApplicationContext());
		InputStream instr;
		List<book> result;
		try {
			instr = this.getAssets().open("index.xml");
			try {
				result=mfile.read_xml(instr);
				for(book mr : result){
					Toast.makeText(getApplicationContext(), mr.toString(), Toast.LENGTH_LONG).show();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值