采用PULL解析XML

8 篇文章 0 订阅
5 篇文章 0 订阅
 
/*
	 * 读取
	 */
	public void testReadPull() throws ParserConfigurationException,
			SAXException, IOException, XmlPullParserException
	{

		PullXml sax = new PullXml();
		InputStream inStream = this.getClass().getClassLoader()
				.getResourceAsStream("secn.xml");
		List<Person> persons = sax.domParser(inStream);
		for (Person person : persons)
		{
			Log.i("SaxTest", String.valueOf(person.getName()));
		}
	}

	/*
	 * 保存
	 */
	public void testReadPullSave() throws ParserConfigurationException,
			SAXException, IOException, XmlPullParserException
	{

		FileOutputStream fos = this.getContext().openFileOutput("pull.xml", Context.MODE_APPEND);
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(1,"xiaoli",(short)12));
		persons.add(new Person(1,"xiaoli",(short)12));
		persons.add(new Person(1,"xiaoli",(short)12));
		persons.add(new Person(1,"xiaoli",(short)12));
		persons.add(new Person(1,"xiaoli",(short)12));
		
		PullXml sax = new PullXml();
		sax.save(persons, fos);
		
		
	}
 /** * * @param persons 集合 * @param outStream 要输出到的地方 * @throws IllegalArgumentException * @throws IllegalStateException * @throws IOException */ public void save(List<Person> persons,OutputStream outStream) throws IllegalArgumentException, IllegalStateException, IOException{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outStream, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "persons"); for(Person person:persons){ serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(person.getId())); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(String.valueOf(person.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); outStream.flush(); outStream.close(); } /** * 解析后并返回一个对象集合 * * @param inStream * xml输入流 * @return 解析后并返回一个对象集合 * @throws ParserConfigurationException * @throws SAXException * @throws IOException * @throws XmlPullParserException */ @SuppressWarnings("static-access") public List<Person> domParser(InputStream inStream) throws ParserConfigurationException, SAXException, IOException, XmlPullParserException { List<Person> persons = null; Person person = null; XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance(); XmlPullParser pullPerser = pullFactory.newPullParser(); pullPerser.setInput(inStream, "utf-8"); int eventType = pullPerser.getEventType(); while(eventType != XmlPullParser.END_DOCUMENT){ switch (eventType) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: if("person".equals(pullPerser.getName())){ person = new Person(); person.setId(new Integer(pullPerser.getAttributeValue(0))); } if("name".equals(pullPerser.getName())){ person.setName(pullPerser.nextText()); }else if("age".equals(pullPerser.getName())){ person.setAge(new Short(pullPerser.nextText())); } break; case XmlPullParser.END_TAG: if("person".equals(pullPerser.getName())){ persons.add(person); } break; } eventType = pullPerser.next(); } return persons; }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值