SAX解析


SAX解析
 
[機能]
1.SAXすなわちorg.xml.sax.helpers.DefaultHandler
2.この例ではGoogle Weatherを例に、http://www.google.com/ig/api?weather=chengdu,chinaにアクセスします.
 
 
 
[コード]
1.クエリーされたデータとそのデータを保存するためのWeatherSetのインタフェースの定義
public class WeatherSet {
	
	String humidity;
	String city;
	
	//construct
	public WeatherSet(){
	}
	
	public void setHumidity(String s){
		humidity = s;
	}
	public String getHumidity(){
		return humidity;
	}
	
	public void setCity(String s){
		city = s;
	}
	public String getCity(){
		return city;
	}
}

 
 
 
2.SAXをカスタマイズし、以下の関数を実現する
//               

*     
public void startDocument()

*     
public void endDocument()

*      <tag >
public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts)

*      </tag >
public void endElement(String namespaceURI, String localName, String qName)

* <tag >   </tag >   
public void characters(char ch[], int start, int length)

 
 次の情報が必要だとします.
<city data="Chengdu, Sichuan" />

<humidity data="  : 76%" />

 
 WeatherHandlerrは次のようになります.
public class WeatherHandler extends org.xml.sax.helpers.DefaultHandler {
	
	WeatherSet weather;
	
	private boolean iteration = false;
	
	public WeatherHandler(WeatherSet set){
		weather = set;
	}
     @Override  // called when Handler begin
     public void startDocument() throws SAXException {
     }

     public void endDocument() throws SAXException {
     }

     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {

    	 if (localName.equals("forecast_information")) {
    		 iteration = true;
          }
    	 else if(localName.equals("city")) {
    		 if(iteration == true){
    			 String attrValue = atts.getValue("data");
    			 weather.setCity(attrValue);
      	   		
    			 iteration = false;
    		 }
    	 }

    	 if (localName.equals("current_conditions")) {
    		 iteration = true;
          }
    	 else if(localName.equals("humidity")) {
    		 if(iteration == true){
    			 String attrValue = atts.getValue("data");
    			 weather.setHumidity(attrValue);
    			 
    			 iteration = false;
    		 }
    	 }
     }
     
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
     }
     
    public void characters(char ch[], int start, int length) {
    }

}

 
 
3.WeatherHandlerの使い方
 
*宛先URLの定義
String city = "chengdu,china";
        String queryString = "http://www.google.com/ig/api?weather="
            + city;

URI uri = new URL(queryString.replace(" ", "%20"));

 
 
*XML Readerを定義し、必要なorg.xml.sax.helpers.DefaultHandlerを指定します.
SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();
			
XMLReader xr = sp.getXMLReader();
			
WeatherHandler handler = new WeatherHandler(weather);

xr.setContentHandler(handler);

 
 
*解析対象URIの開始
xr.parse(new InputSource(uri.openStream()));

 
 
4.その他の問題
*権限の問題:
<uses-permission android:name="android.permission.INTERNET" />

 
*実行結果:
  SAX 解析