`

Pull解析

    博客分类:
  • XML
 
阅读更多
1.首先在SD卡上新建一个测试文件夹
// 获取保存路径
	public File getFilePath() {
		File filePath = null;
		//判断SD卡存在与否
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			filePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/A_Test/test/");
			if (!filePath.isDirectory()) {//判断文件存在与否,不存在就创建
				filePath.mkdirs();
			}	
		} else {
			Toast.makeText(Main.this, "存储卡不存在,请插入卡!", 3000).show();
		}
		return filePath;
	}

注意:权限的添加:
<!-- SD卡写入 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
2.生成一个测试的XML文件
File file = new File(getFilePath() + "/test.xml");
if (!file.exists()) {// 如果文件不存在,就创建新文件
				System.out.println("文件不存在,马上创建!");
				List<PersonModel> personModel = new ArrayList<PersonModel>();
				personModel.add(new PersonModel(1001, "张三", (short) 30));
				personModel.add(new PersonModel(1002, "李四", (short) 18));
				personModel.add(new PersonModel(1003, "王二", (short) 21));
				CreateXML.createXML(file, personModel);
}
/**
 * 生成XML文件
 * 
 * @author lilin
 * @date 2011-9-26 上午08:36:08
 * @ClassName: CreateXML
 * @Description: TODO
 */
public class CreateXML {
	// 生成XML文件
	public static void createXML(File file, List<PersonModel> personModel)
			throws IllegalArgumentException, IllegalStateException, IOException {
		FileOutputStream outputStream = null;
		OutputStreamWriter writer = null;
		outputStream = new FileOutputStream(file);
		writer = new OutputStreamWriter(outputStream, "UTF-8");
		BufferedWriter writer2 = new BufferedWriter(writer);// 增加的缓存功能
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(writer2);
		serializer.setOutput(outputStream, "utf-8");
		serializer.startDocument("utf-8", true);
		serializer.startTag(null, "persons");
		for (PersonModel persons : personModel) {
			serializer.startTag(null, "person");
			serializer.attribute(null, "id", persons.getId().toString());
			
			serializer.startTag(null, "name");
			serializer.text(persons.getName());
			serializer.endTag(null, "name");
			
			serializer.startTag(null, "age");
			serializer.text(persons.getAge().toString());
			serializer.endTag(null, "age");
			
			serializer.endTag(null, "person");
		}
		serializer.endTag(null, "persons");
		serializer.endDocument();
		outputStream.close();
	}
}
3.解析XML
FileInputStream fileInputStream = new FileInputStream(file);
			List<PersonModel> personModel = PullXML.pullXML(fileInputStream);
			for (int i = 0; i < personModel.size(); i++) {
				System.out.println(personModel.get(i).getId() + "--"
						+ personModel.get(i).getName() + "--"
						+ personModel.get(i).getAge());
			}

/**
 * 解析XML
 * 
 * @author lilin
 * @date 2011-9-26 上午09:23:37
 * @ClassName: PullXML
 * @Description: TODO
 */
public class PullXML {
	public static List<PersonModel> pullXML(InputStream inStream)
			throws Throwable {
		List<PersonModel> persons = null;
		PersonModel person = null;
		/* 声明XML的pull解析器 */
		XmlPullParser parser = Xml.newPullParser();
		/* 声明编码方式 */
		parser.setInput(inStream, "UTF-8");
		int eventType = parser.getEventType();// 产生第一个事件
		while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:// 0 文档开始事件
				persons = new ArrayList<PersonModel>();
				break;

			case XmlPullParser.START_TAG:// 2 开始元素
				String name = parser.getName();// 获取解析器当前指向的元素的名称
				if ("person".equals(name)) {
					person = new PersonModel();
					person.setId(new Integer(parser.getAttributeValue(0)));
				}
				if (person != null) {
					if ("name".equals(name)) {
						person.setName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
					}
					if ("age".equals(name)) {
						person.setAge(new Short(parser.nextText()));
					}
				}
				break;

			case XmlPullParser.END_TAG:
				if ("person".equals(parser.getName())) {
					persons.add(person);
					person = null;
				}
				break;
			}
			eventType = parser.next();
		}
		return persons;
	}
}

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics