Back to project page Weather.
The source code is released under:
GNU General Public License
If you think the Android project Weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.imlongluo.weather.utils; // www . ja v a2s . c o m import java.util.HashMap; import java.util.Map; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * * @author longluo * ???????XMl???????????????? */ public class LocationXMLParser extends DefaultHandler { //????????CountryName???,?? public static final int COUNTRYNAME=1; //???????AdministrativeAreaName????? public static final int ADMINISTRATIVEAREANAME =2; //???????LocalityName?? ?? public static final int LOCALITYNAME =3; //???????DependentLocalityName?? ?? public static final int DEPENDENTLOCALITYNAME = 4; //???????????? private boolean hasAddress= false; //??????????????? private int element=0; //???????????????Map private Map<Integer, String> locationInfo =new HashMap<Integer, String>(); @Override public void characters(char[] ch, int start, int length) throws SAXException { if(hasAddress) { //??address????????????????????????????????? if(ch==null||ch.length==0) { hasAddress = false; } } //??????? String text = new String(ch); //????????? if(hasAddress) { switch(element) { case COUNTRYNAME: text = text.substring(start, start+length); locationInfo.put(COUNTRYNAME, text); break; case ADMINISTRATIVEAREANAME: //?????1???????????????? text = text.substring(start, start+length-1); locationInfo.put(ADMINISTRATIVEAREANAME, text); break; case LOCALITYNAME: //?????1???????????????? text = text.substring(start, start+length-1); locationInfo.put(LOCALITYNAME, text); break; case DEPENDENTLOCALITYNAME: //?????1???????????????? text = text.substring(start, start+length-1); locationInfo.put(DEPENDENTLOCALITYNAME, text); break; default: break; } } } @Override public void endDocument() throws SAXException { super.endDocument(); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); } @Override public void startDocument() throws SAXException { super.startDocument(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); //???<address>??? if(localName.equals("address")) { hasAddress = true; } else if(localName.equals("CountryName")) { element = COUNTRYNAME; } else if(localName.equals("AdministrativeAreaName")) { element = ADMINISTRATIVEAREANAME; } else if(localName.equals("LocalityName")) { element = LOCALITYNAME; } else if(localName.equals("DependentLocalityName")) { element = DEPENDENTLOCALITYNAME; } else { element = 0; } } //???????????? public boolean hasAddress() { return hasAddress; } //????????????? public Map<Integer, String> getDetailAddress() { return locationInfo; } }