Android Open Source - Weather Location X M L Parser






From Project

Back to project page Weather.

License

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.

Java Source Code

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;
  }
}




Java Source Code List

.WebAccessTools.java
com.imlongluo.weather.app.MainActivity.java
com.imlongluo.weather.app.SetCityActivity.java
com.imlongluo.weather.app.UpdateWidgetService.java
com.imlongluo.weather.app.WeatherWidget.java
com.imlongluo.weather.db.DBHelper.java
com.imlongluo.weather.location.GPSListAdapter.java
com.imlongluo.weather.location.MyListAdapter.java
com.imlongluo.weather.utils.LocationXMLParser.java
com.imlongluo.weather.utils.WeaterInfoParser.java