首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

XmlUtil工具种

2012-12-25 
XmlUtil工具类package com.sinosoft.commonimport java.io.IOExceptionimport java.io.InputStreamimpo

XmlUtil工具类

package com.sinosoft.common;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List; import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import android.content.Context;import android.util.Xml;import com.sinosoft.model.SpinnerVO;public class XmlUtil {public static List<SpinnerVO> parse(Context context, String xmlPath) {List<SpinnerVO> SpinnerVOs = new ArrayList<SpinnerVO>();SpinnerVO SpinnerVO = null;InputStream inputStream = null; XmlPullParser xmlParser = Xml.newPullParser();try { inputStream = context.getResources().getAssets().open(xmlPath);xmlParser.setInput(inputStream, "UTF-8"); int evtType = xmlParser.getEventType(); while (evtType != XmlPullParser.END_DOCUMENT) {switch (evtType) {case XmlPullParser.START_TAG:String tag = xmlParser.getName(); if (tag.equalsIgnoreCase("SpinnerVO")) { SpinnerVO = new SpinnerVO(); } else if (SpinnerVO != null) { if (tag.equalsIgnoreCase("code")) {SpinnerVO.setCode(xmlParser.nextText());} else if (tag.equalsIgnoreCase("name")) {SpinnerVO.setName(xmlParser.nextText());}}break;case XmlPullParser.END_TAG:if (xmlParser.getName().equalsIgnoreCase("SpinnerVO") && SpinnerVO != null) {SpinnerVOs.add(SpinnerVO);SpinnerVO = null;}break;default:break;}evtType = xmlParser.next();}} catch (XmlPullParserException e) { e.printStackTrace();} catch (IOException e1) { e1.printStackTrace();}return SpinnerVOs;}}


使用范例:
List<SpinnerVO> spinnerVOs = XmlUtil.parse(MainActivity.this, "LicencePlateKind.xml");

热点排行