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");