Android之xml解析2,3

package com.hnust.util;

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.hnust.pojo.Person;

/***
 * dom解析的工具类
 * @author zzg
 *
 */
public class DOMXMLService {

	private final static String TAG="DOMXMLService";
	public static List<Person> getPerson(InputStream is) throws Exception{
		List<Person> persons=new ArrayList<Person>();
		//获取dom解析的工厂实例
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse(is);
		//返回文档的根元素
		Element root=document.getDocumentElement();
		//获取person节点的元素
		NodeList personNode=root.getElementsByTagName("person");
		for(int i=0;i<personNode.getLength();i++){
			Element personElement = (Element) personNode.item(i);
			int id=new Integer(personElement.getAttribute("id"));
			Log.i(TAG, id+"");
			Person person=new Person();
			person.setId(id);
			NodeList childNodes=personElement.getChildNodes();
			for(int j=0;j<childNodes.getLength();j++){
				if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){
					if("name".equals(childNodes.item(j).getNodeName())){
						String name=childNodes.item(j).getFirstChild().getNodeValue();
						Log.i(TAG, name);
						person.setName(name);
					}else if("age".equals(childNodes.item(j).getNodeName())){
						String age=childNodes.item(j).getFirstChild().getNodeValue();
						Log.i(TAG, age);
						person.setAge(Integer.parseInt(age));
					}
				}
			}
			persons.add(person);
		}
		is.close();
		return persons;
	}
}
package com.hnust.util;

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

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

import com.hnust.pojo.Person;

/**
 * pull解析xml工具类
 * pull解析器为Android内置的
 * @author zzg
 *
 */
public class PullXMLService {

	public static List<Person> getPerson(InputStream is) throws  Exception{
		Person person=null;
		List<Person> persons=null;
		XmlPullParser pullParser=Xml.newPullParser();
		pullParser.setInput(is,"UTF-8");
		//触发时间类型
		int event=pullParser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){//跳出循环的条件是结束解析
			switch (event) {
			case XmlPullParser.START_DOCUMENT://开始解析
				persons=new ArrayList<Person>();
				break;
			case XmlPullParser.START_TAG://开始元素
				if("person".equals(pullParser.getName())){
					int id=Integer.parseInt(pullParser.getAttributeValue(0));
					person=new Person();
					person.setId(id);
				}
				if(person!=null){
					if("name".equals(pullParser.getName())){
						person.setName(pullParser.nextText());
					}
					if("age".equals(pullParser.getName())){
						person.setAge(Integer.parseInt(pullParser.nextText()));
					}
				}
				break;
			case XmlPullParser.END_TAG:
				if("person".equals(pullParser.getName())){
					persons.add(person);
					person=null;
				}
				break;
			default:
				break;
			}
			event=pullParser.next();
			
		}
		return persons;
	}
	/***
	 * pull生成xml
	 * @param persons
	 * @param out
	 * @throws Exception
	 */
	public static void save(List<Person> 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",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, "name");
			
			serializer.endTag(null, "person");
		}
		serializer.endTag(null,"persons");
		serializer.endDocument();
		out.flush();
		out.close();
	}
}
	AssetManager asset=getAssets();
			InputStream is=asset.open("person.xml");
			List<Person> persons=null;
			StringBuilder sb=new StringBuilder();
			switch(v.getId()){
			case R.id.sax_button:
				//sax解析xml文件
				SAXForHandler handler=new SAXForHandler();
				SAXParserFactory spf=SAXParserFactory.newInstance();
				SAXParser parser=spf.newSAXParser();
				parser.parse(is, handler);
				
				persons=handler.getPerson();
				for(Person person:persons){
					sb.append(person.toString());
				}
				Toast.makeText(this, sb,1).show();
				break;
			case R.id.pull_button:
				persons=PullXMLService.getPerson(is);
				if(persons.size()==0){
					Toast.makeText(this, "persons is null",1).show();
				}
				for(Person person:persons){
					sb.append(person.toString());
				}
				Toast.makeText(this, sb,1).show();
				break;
			case R.id.dom_button:
				//dom解析xml文件
				persons=DOMXMLService.getPerson(is);
				if(persons.size()==0){
					Toast.makeText(this, "persons is null",1).show();
				}
				for(Person person:persons){
					sb.append(person.toString());
				}
				Toast.makeText(this, sb,1).show();
				break;



你可能感兴趣的:(Android之xml解析2,3)