04 pull生成XML

前端之家收集整理的这篇文章主要介绍了04 pull生成XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.example.lession04_pull.service;

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

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.os.Environment;
import android.util.Xml;

import com.example.lession04_pull.domain.Person;

public class PullXmlService {
	
	public Person currentPerson;

	public List<Person> readXml(InputStream is){
		//声明返回值
		List<Person> persons=null;
		//首先利用Xml.newPullParser()获取解析对象
		XmlPullParser xmlPullParser=Xml.newPullParser();
		try{
			// 解析文件
			xmlPullParser.setInput(is,"UTF-8");
			//获取解析的事件类型
			int eventType=xmlPullParser.getEventType();
			//判断文件解析的是否完毕
			while(eventType!=XmlPullParser.END_DOCUMENT){
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					persons=new ArrayList<Person>();
					break;

				case XmlPullParser.START_TAG:
					String tagName=xmlPullParser.getName();
					if("person".equals(tagName)){
						//创建person对象
						currentPerson=new Person();
						currentPerson.setId(Integer.parseInt(xmlPullParser
								.getAttributeValue(null,"id")));
					}else if("name".equals(tagName)){
						currentPerson.setName(xmlPullParser.nextText());
					}else if("age".equals(tagName)){
						currentPerson.setAge(new Short(xmlPullParser.nextText()));
					}
					break;
				case XmlPullParser.END_TAG:
					if("person".equals(xmlPullParser.getName()) && currentPerson!=null){
						//把person对象放到集合中去
						persons.add(currentPerson);
						currentPerson=null;
					}
					break;
				}
				eventType=xmlPullParser.next();
			}
			is.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return persons;
	}
	
	
	
	// 写入
		public boolean write(List<Person> persons) {
			// 采用pull解析进行实现

			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {

				// 获取sdcard目录 文件对象
				File sdCardDir = Environment.getExternalStorageDirectory();
				// 创建文件
				File file = new File(sdCardDir,"mycsdn.xml");

				XmlSerializer serializer = Xml.newSerializer();

				FileOutputStream fos = null;
				try {
					// 根据文件对象创建一个文件输出流对象
					fos = new FileOutputStream(file);
					// 设置输出的流及编码
					serializer.setOutput(fos,"UTF-8");
					// 设置文件的开始
					serializer.startDocument("UTF-8",true);
					// persons标签开始
					serializer.startTag(null,"persons");
					for (Person person : persons) {
						// person标签的开始
						serializer.startTag(null,"person");
						// 设置person标签属性
						serializer.attribute("null","id",person.getId() + "");

						// 设置person标签的子标签 name
						serializer.startTag(null,"name");
						serializer.text(person.getName());
						serializer.endTag(null,"name");

						// 设置person标签的子标签的age
						serializer.startTag(null,"age");
						serializer.text(person.getAge() + "");
						serializer.endTag(null,"age");

						// person标签的结束
						serializer.endTag(null,"person");
					}

					// persons标签的结束
					serializer.endTag(null,"persons");
					// 文件的结束
					serializer.endDocument();

					fos.flush();
					fos.close();
					return true;
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			return false;
		}

}
原文链接:https://www.f2er.com/xml/298223.html

猜你在找的XML相关文章