Android xml解析

package cn.itcast.mobilesafe.engine;

import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import cn.itcast.mobilesafe.domain.UpdataInfo;

public class UpdataInfoParser {

	/**
	 * 
	 * @param is
	 *            解析的xml的inputstream 如果项目比较大 最好 把解析的数据 放到各自的解析器 用来专门把String等转为 需要的对象 供 service调用
	 * @return updateinfo
	 */
	public static UpdataInfo getUpdataInfo(InputStream is) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		UpdataInfo info = new UpdataInfo();
		parser.setInput(is, "utf-8");
		int type = parser.getEventType();

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
			case XmlPullParser.START_TAG:
				if("version".equals(parser.getName())){
					String version = parser.nextText();
					info.setVersion(version);
				}else if("description".equals(parser.getName())){
					String description = parser.nextText();
					info.setDescription(description);
				}else if("apkurl".equals(parser.getName())){
					String apkurl = parser.nextText();
					info.setApkurl(apkurl);
				}
				
				break;

			}

			type = parser.next();
		}
		return info;
	}

}

posted @ 2012-12-05 21:16  sfshine  阅读(218)  评论(0编辑  收藏  举报