SAX,本地解析:
try { SAX(getAssets().open("data.xml")); } catch (Exception e) { e.printStackTrace(); }
public void SAX(InputStream input) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xReader = sp.getXMLReader(); XMLAdressInfo xmlAdress = new XMLAdressInfo(); xReader.setContentHandler(xmlAdress); xReader.parse(new InputSource(input)); //获得所有的数据 hashMap = xmlAdress.getData(); //获得省市对应的数据 provinceCity = xmlAdress.getCityData(); Log.d("dfdfd", ""+provinceCity.size()); }
public class XMLAdressInfo extends DefaultHandler{ private String characters = ""; private HashMap<String, HashMap<String, List<String>>> hashMap ; private HashMap<String, List<String>> hashMapCity ; private List<String> list; private String city; private String province; private HashMap<String, List<String>> provinceCity ; private List<String> cityList ; @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); String str = new String(ch, start, length); str = str.replace("\n", ""); str = str.replace("\t", ""); characters += str.trim(); } @Override public void startDocument() throws SAXException { super.startDocument(); hashMap = new HashMap<String, HashMap<String,List<String>>>(); provinceCity = new HashMap<String, List<String>>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if(localName.equals("province")){ province = attributes.getValue("name"); hashMapCity = new HashMap<String, List<String>>(); cityList = new ArrayList<String>(); } if(localName.equals("city")){ list = new ArrayList<String>(); city = attributes.getValue("name"); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if(localName.equals("province")){ hashMap.put(province, hashMapCity); provinceCity.put(province, cityList); }else if(localName.equals("city")){ hashMapCity.put(city, list); cityList.add(city); }else if(localName.equals("district")){ list.add(characters); } characters = ""; } public HashMap<String, HashMap<String, List<String>>> getData(){ return hashMap; } public HashMap<String, List<String>> getCityData(){ return provinceCity; } }
PULL 解析:
PpdAdInfo ppdAdInfo = null ; try { XmlPullParser parser = Xml.newPullParser(); // XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); // parser.setInput(new StringReader(strResult));// 获得解析到的事件类别,这里有开始文档,结束文档,开始标签,结束标签,文本等等事件。 parser.setInput(StringTOInputStream(strResult), "UTF-8"); int eventType = parser.getEventType(); ppdAdInfosList = new ArrayList<PpdAdInfo>(); while (eventType != XmlPullParser.END_DOCUMENT) {// 一直循环,直到文档结束 switch (eventType) { case XmlPullParser.START_TAG: if (parser.getName().equals("ppdAd")) { ppdAdInfo = new PpdAdInfo(); } else if (parser.getName().equals("androidPackageName")) { eventType = parser.next(); ppdAdInfo.setAndroidPackageName(parser.getText()); } else if (parser.getName().equals("clickProxyURL")) { eventType = parser.next(); ppdAdInfo.setClickProxyURL(parser.getText()); } else if (parser.getName().equals("ppdCampaignId")) { eventType = parser.next(); ppdAdInfo.setPpdCampaignId(parser.getText()); } else if (parser.getName().equals("productThumbnail")) { eventType = parser.next(); ppdAdInfo.setProductThumbnail(parser.getText()); } else if (parser.getName().equals("productDescription")) { eventType = parser.next(); ppdAdInfo.setProductDescription(parser.getText()); } else if (parser.getName().equals("productName")) { eventType = parser.next(); ppdAdInfo.setProductName(parser.getText()); } else if (parser.getName().equals("rating")) { eventType = parser.next(); ppdAdInfo.setRating(parser.getText()); } break; case XmlPullParser.END_TAG: if (parser.getName().equals("ppdAd")) { ppdAdInfosList.add(ppdAdInfo); ppdAdInfo = null; } break; } eventType = parser.next(); } } catch (Exception e) { Log.d("test", "eeee__" + e.getMessage()); e.printStackTrace(); }
/** * 将String转换成InputStream * @param in * @return * @throws Exception */ public static InputStream StringTOInputStream(String in) throws Exception{ ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1")); return is; }