Android学习之XML Pull 解析

MyXMLPullUtil.java:

package com.cz.xmlpull;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

public class MyXMLPullUtil {

	private List<LinkMan> all = null;
	private OutputStream output = null;

	private InputStream input = null;

	public MyXMLPullUtil(OutputStream output, List<LinkMan> all) {
		this.output = output;
		this.all = all;
	}

	public MyXMLPullUtil(InputStream input) {
		this.input = input;
	}

	public void writeByXMLPull() throws Exception {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlSerializer xs = factory.newSerializer();
		xs.setOutput(this.output, "UTF-8");
		xs.startDocument("UTF-8", true);
		xs.startTag(null, "addresslist");// 根元素
		Iterator<LinkMan> iter = this.all.iterator();
		while (iter.hasNext()) {
			LinkMan man = iter.next();
			xs.startTag(null, "linkman");
			xs.startTag(null, "name");
			xs.text(man.getName());
			xs.endTag(null, "name");
			xs.startTag(null, "email");
			xs.text(man.getEmail());
			xs.endTag(null, "email");
			xs.endTag(null, "linkman");
		}
		xs.endTag(null, "addresslist");
		xs.endDocument();
		xs.flush();
	}

	public List<LinkMan> readByXMLPull() throws Exception {
		List<LinkMan> all = null;
		LinkMan man = null;
		String elementName = null; // 保存节点的名称
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();// 取得XmlPullParserFactory类对象
		XmlPullParser xmlPullParser = factory.newPullParser();// 取得XmlPullParser接口对象
		xmlPullParser.setInput(this.input, "UTF-8");
		int eventType = xmlPullParser.getEventType(); // 取得事件码
		while (eventType != XmlPullParser.END_DOCUMENT) { // 不是文档底部
			if (eventType == XmlPullParser.START_DOCUMENT) { // 文档开始
				all = new ArrayList<LinkMan>();
			} else if (eventType == XmlPullParser.START_TAG) { // 元素标记开始
				elementName = xmlPullParser.getName(); // 取得元素的名称
				if ("linkman".equals(elementName)) {
					man = new LinkMan();
				}
			} else if (eventType == XmlPullParser.END_TAG) { // 结束元素
				elementName = xmlPullParser.getName(); // 取得节点名称
				if ("linkman".equals(elementName)) {
					all.add(man);
					man = null;
				}
			} else if (eventType == XmlPullParser.TEXT) { // 数据
				if ("name".equals(elementName)) {
					man.setName(xmlPullParser.getText());
				} else if ("email".equals(elementName)) {
					man.setEmail(xmlPullParser.getText());
				}
			}
			eventType = xmlPullParser.next(); // 取得下一个事件码
		}
		return all;
	}
}

XMLPullActivity.java:

package com.cz.xmlpull;

import java.io.File;
import java.io.FileInputStream;
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 android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.cz.test.R;

public class XMLPullActivity extends Activity {

	private TextView name = null;
	private TextView email = null;
	private Button but_read = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.haha);
		
		writeByXMLPull();
		
		readByXMLPull();

	}

	private void writeByXMLPull() {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) { // 不存在不操作
			return; // 返回到程序的被调用处
		}
		File file = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "haha" + File.separator + "test.xml"); // 要输出文件的路径
		if (!file.getParentFile().exists()) { // 文件不存在
			file.getParentFile().mkdirs(); // 创建文件夹
		}
		List<LinkMan> all = new ArrayList<LinkMan>();
		for (int x = 0; x < 3; x++) {
			LinkMan man = new LinkMan();
			man.setName("haha - " + x);
			man.setEmail("[email protected]");
			all.add(man);
		}
		OutputStream output = null;
		try {
			output = new FileOutputStream(file);
			new MyXMLPullUtil(output, all).writeByXMLPull();
		} catch (Exception e) {
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private void readByXMLPull() {
		this.name = (TextView) super.findViewById(R.id.name);
		this.email = (TextView) super.findViewById(R.id.email);
		this.but_read = (Button) super.findViewById(R.id.but);
		this.but_read.setOnClickListener(new OnClickListenerImpl());
	}

	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) { // 不存在不操作
				return; // 返回到程序的被调用处
			}
			File file = new File(Environment.getExternalStorageDirectory()
					+ File.separator + "xdwang" + File.separator + "test.xml"); // 要输出文件的路径
			if (!file.exists()) { // 文件不存在
				return;
			}
			try {
				InputStream input = new FileInputStream(file);
				MyXMLPullUtil util = new MyXMLPullUtil(input);
				List<LinkMan> all = util.readByXMLPull();
				XMLPullActivity.this.name.setText(all.get(0).getName());
				XMLPullActivity.this.email.setText(all.get(0).getEmail());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}

扫描二维码关注公众号,回复: 594846 查看本文章

LinkMan.java:

package com.cz.xmlpull;

public class LinkMan {

	private String name;
	private String email;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}

猜你喜欢

转载自chenzheng8975.iteye.com/blog/2033920