Xml解析之----Pull
解析Xml有三种方式可选,sax ,dom ,pull?。? Dom在android上性能不是很少,在android上首选sax和pull。
<string>直辖市</string>
<string>特别行政区</string>
<string>黑龙江</string>
<string>吉林</string>
<string>辽宁</string>
<string>内蒙古</string>
<string>河北</string>
<string>河南</string>
<string>山东</string>
<string>山西</string>
<string>江苏</string>
<string>安徽</string>
<string>陕西</string>
<string>宁夏</string>
<string>甘肃</string>
<string>青海</string>
</ArrayOfString>
public static ArrayList<String> getProvince(InputStream inStream, int type) {ArrayList<String> data = new ArrayList<String>();XmlPullParser xmlPullParser = Xml.newPullParser();try { xmlPullParser.setInput(inStream, "UTF-8"); int eventCode = xmlPullParser.getEventType(); while (eventCode != XmlPullParser.END_DOCUMENT) {String name = xmlPullParser.getName();switch (eventCode) {case XmlPullParser.START_DOCUMENT:// 文档开始 break;case XmlPullParser.START_TAG:// 元素开始. if (name.equalsIgnoreCase("string")) {if(type ==2) data.add(xmlPullParser.nextText() .split("\\(")[0].trim().toString());else { data.add(xmlPullParser.nextText() );} } break;case XmlPullParser.END_TAG:// 元素结束 break;case XmlPullParser.END_DOCUMENT:// 文档结束 break;default: break;}eventCode = xmlPullParser.next(); }} catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace();} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();}
?
?
?