Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android采用pull解析器解析和生成XML

android采用pull解析器解析和生成XML

編輯:關於Android編程

在Android上解析XML文件可以采用SAX、DOM和Android的自帶的pull。其中pull最方便和推薦使用:

對於一個XML文件中的內容,要定制一個JAVA BEAN來接收,先看下.XML文件的內容:



	
		liming
		30
	
	
		zhangxiaoxiao
		25
	

看下解析方法:

	/**
	 * 解析XML
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	public List getPersons(InputStream xml)throws Exception{
		List persons = null;
		Person person = null;
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(xml,"UTF-8");//為pull解析器設置要解析的數據
		int event = pullParser.getEventType();
		while(event != XmlPullParser.END_DOCUMENT){
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList();
				break;
			case XmlPullParser.START_TAG:
				if("person".equals(pullParser.getName())){
					int id = new Integer(pullParser.getAttributeValue(0));
					person = new Person();
					person.setId(id);
				}
				if("name".equals(pullParser.getName())){
					String name = pullParser.nextText();
					person.setName(name);
				}
				if("age".equals(pullParser.getName())){
					int  age = new Integer(pullParser.nextText());
					person.setAge(age);
				}
				break;
			case XmlPullParser.END_TAG:
				if("person".equals(pullParser.getName())){
					persons.add(person);
					person = null;
				}
				break;
			}
			event = pullParser.next();
		}
		return persons;
	}
測試方法:

public void testPerson()throws Exception{
		InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");
		PersonService ps = new PersonService();
		List persons = ps.getPersons(xml);
		for (Person person : persons) {
			Log.i(TAG,person.toString());
		}
	}
再看下生成XML文件:

	/**
	 * 保存
	 * @param persons
	 * @param out
	 * @throws Exception
	 */
	public void save(List persons,OutputStream out)throws Exception{
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(out,"UTF-8");
		serializer.startDocument("UTF-8", true);
		serializer.startTag(null, "persons");
		for (Person person : persons) {
			serializer.startTag(null, "person");
			serializer.attribute(null, "id", person.getId().toString());
			
			serializer.startTag(null, "name");
			serializer.text(person.getName());
			serializer.endTag(null, "name");
			
			serializer.startTag(null, "age");
			serializer.text(person.getAge().toString());
			serializer.endTag(null, "age");
			
			serializer.endTag(null, "person");
		}
		
		serializer.endTag(null, "persons");
		serializer.endDocument();
		out.flush();
		out.close();
	}

看下生成的測試方法:

public void testSave()throws Exception{
		PersonService ps = new PersonService();
		List persons = new ArrayList();
		persons.add(new Person(43,"zhangxx",80));
		persons.add(new Person(12,"lili",20));
		persons.add(new Person(15,"xiaoxiao",8));
		File xmlFile = new File(getContext().getFilesDir(),"itcase.xml");
		FileOutputStream outputStream = new FileOutputStream(xmlFile);
		ps.save(persons, outputStream);
	}




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved