Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android:xml解析

android:xml解析

編輯:關於Android編程



	
		Liri
		30
	
	
		Liyue
		25
	


package org.fun.domain;

public class Person {
	private Integer id;
	private String name;
	private Short age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public Short getAge() {
		return age;
	}

	public void setAge(Short age) {
		this.age = age;
	}

}


package org.fun.service;

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

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.fun.domain.Person;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXPersonService {
	public List getPersons(InputStream inStream) throws Throwable {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		PersonParser personParser = new PersonParser();
		parser.parse(inStream, personParser);
		return personParser.getPersons();
	}

	private final class PersonParser extends DefaultHandler {

		private List persons = null;
		private String tag = null;
		private Person person = null;

		public List getPersons() {
			return persons;
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (tag != null) {
				String data = new String(ch, start, length);
				if ("name".equals(tag)) {
					person.setName(data);
				} else if ("age".equals(tag)) {
					person.setAge(new Short(data));
				}
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if ("person".equals(localName)) {
				persons.add(person);
				person = null;
			}
			tag = null;
		}

		@Override
		public void startDocument() throws SAXException {
			persons = new ArrayList();
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if ("person".equals(localName)) {
				person = new Person();
				person.setId(new Integer(attributes.getValue(0)));
			}
			tag = localName;
		}

	}
}


package org.fun.service;

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.fun.domain.Person;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMPersonService {
	public static List getPersons(InputStream inStream)
			throws Throwable {
		List persons = new ArrayList();

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(inStream);
		Element root = document.getDocumentElement();
		NodeList personNodes = root.getElementsByTagName("person");
		for (int i = 0; i < personNodes.getLength(); i++) {
			Person person = new Person();
			Element personElement = (Element) personNodes.item(i);
			person.setId(new Integer(personElement.getAttribute("id")));
			NodeList personChilds = personElement.getChildNodes();
			for (int j = 0; j < personNodes.getLength(); j++) {
				if (personChilds.item(j).getNodeType() == Node.ELEMENT_NODE) {
					Element childElement = (Element) personChilds.item(j);
					if ("name".equals(childElement.getNodeName())) {
						person.setName(childElement.getFirstChild()
								.getNodeValue());
					} else if ("age".equals(childElement.getNodeName())) {
						person.setAge(new Short(childElement.getFirstChild()
								.getNodeValue()));
					}
				}
			}
			persons.add(person);
		}

		return persons;
	}
}


package org.fun.service;

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

import org.fun.domain.Person;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

public class PullPersonService {

	/*====================pull生成xml文件
	public static void save(List persons, OutputStream outputStream)
			throws Throwable {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(outputStream, "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();
		outputStream.flush();
		outputStream.close();
	}
	 =====================================*/

	public static List getPersons(InputStream inStream)
			throws Throwable {
		List persons = null;
		Person person = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "utf-8");
		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList();
				break;
			case XmlPullParser.START_TAG:
				String name = parser.getName();
				if ("person".equals(name)) {
					person = new Person();
					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;
			default:
				break;
			}
			eventType = parser.next();
		}
		return persons;
	}
}




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