Android采用SAX解析XML文档_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android采用SAX解析XML文档

Android采用SAX解析XML文档

 2010/12/15 8:00:59  mzba520  http://mzba520.javaeye.com  我要评论(0)
  • 摘要:packagecom.cn.service;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax
  • 标签:android 文档 XML文档

package com.cn.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.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.cn.domain.Persion;
/****
 * 采用SAX解析XML
 * 
 * ****/
public class SAXPersionService {
	
	public List<Persion> getPersino(InputStream is)throws Exception{
		SAXParserFactory factory = SAXParserFactory.newInstance();  
		SAXParser parser = factory.newSAXParser();
		PersionHandler handler = new PersionHandler();
		parser.parse(is, handler);
		return handler.getList();
	}
	
	private final class PersionHandler extends DefaultHandler{
		private List<Persion> list = null;
		private Persion persion;
		private String tag;
		
		public List<Persion> getList() {
			return list;
		}
		@Override
		public void startDocument() throws SAXException {
			list = new ArrayList<Persion>();
		}
		@Override
		public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
			if("person".equals(localName)){
				persion = new Persion();
				persion.setId(Integer.parseInt(attributes.getValue(0)));
			}
			tag = localName;
		}
		@Override
		public void characters(char[] ch, int start, int length)throws SAXException {
			if(tag != null){
				String textdata = new String(ch,start,length);
				if("name".equals(tag)){
					persion.setName(textdata);
				}else if("age".equals(tag)){
					persion.setAge(Short.parseShort(textdata));
				}
			}
		}
		@Override
		public void endElement(String uri, String localName, String qName)throws SAXException {
			tag = null;
			if("person".equals(localName)){
				list.add(persion);
				persion = null;
			}
		}
		
	}
}

下面是类Persion的定义:


package com.cn.domain;

public class Persion {
	private int id;
	private String name;
	private short age;
	
	public Persion(){
		
	}
	
	public Persion(int id, String name, short age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int 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;
	}
	@Override
	public String toString() {
		return "Persion [age=" + age + ", id=" + id + ", name=" + name + "]";
	}
}
?
上一篇: TableView 的使用 实例一 下一篇: 编译 ZXing
发表评论
用户名: 匿名