SAX PULL 분석
6724 단어 pull
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("
", "");
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SAX PULL 분석SAX, 로컬 확인: PULL 확인:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.