Scrapbook/개발 및 프로그래밍

네트워크에서 XML 파일 가져와 파싱하기

가을이짱짱 2011. 1. 13. 17:26
반응형


기상청 데이터 파싱 예
XmlPullParserFactory, XmlPullParser

public class PullParserActivity extends ListActivity {
 ImageView conditionImage;
 TextView regionTextView;
 TextView conditionTextView;
 
 ArrayList<Weather> weather = new ArrayList<Weather>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
   URL url = new URL("http://www.kma.go.kr/XML/weather/sfc_web_map.xml");
   XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
   XmlPullParser parser = parserFactory.newPullParser();
   parser.setInput(url.openStream(), "utf-8");
     int parserEvent = parser.getEventType();
   String tag="ready..!";

   String stnid="";
   String desc="";
   String ta="";
   String region="";
  
   while(parserEvent != XmlPullParser.END_DOCUMENT){
   
    switch(parserEvent){
    case XmlPullParser.START_DOCUMENT:
     Log.d("parserTest", "Parser Start..!");
     break;
    case XmlPullParser.START_TAG:
     tag = parser.getName(); //위치가 중요하다
     if(tag.equals("local")){
      stnid = parser.getAttributeValue(null, "stn_id");
      desc = parser.getAttributeValue(null, "desc");
      ta = parser.getAttributeValue(null, "ta");
     }
     break;
    case XmlPullParser.TEXT:
     //tag = parser.getName(); 여기 있으면 안된다
     if(tag.equals("local")){
      region = parser.getText();
     }
     break;
    
    case XmlPullParser.END_TAG:
     if(tag.equals("local")){
      Weather imsi = new Weather(region,desc,ta,stnid);
      weather.add(imsi);
     }
     tag = "nothing";
     break;
    }
    parserEvent = parser.next();
   }
  
  }  catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Log.d("parserTest","error");
  }
      Log.d("parserTest", weather.get(2).stnid);
      Log.d("parserTest", weather.get(3).stnid);
      Log.d("parserTest", weather.get(4).stnid);

      setListAdapter(new CustomRow(this));
    }
   
    class CustomRow extends ArrayAdapter<Weather>{
     Activity context;
  public CustomRow(Activity c) {
   super(c,R.layout.customcell,weather);
   this.context = c;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
 
    LayoutInflater inf=context.getLayoutInflater();
    View customcell=inf.inflate(R.layout.customcell, null);
   
    conditionImage = (ImageView)customcell.findViewById(R.id.conditionImage);
    regionTextView = (TextView)customcell.findViewById(R.id.TextView03);
    conditionTextView = (TextView)customcell.findViewById(R.id.TextView05);
   
    regionTextView.setText(weather.get(position).region);
    conditionTextView.setText(weather.get(position).desc);
   
    Drawable sunnyImage = getResources().getDrawable(R.drawable.sunny);
    Drawable cloudImage = getResources().getDrawable(R.drawable.cloud);
    Drawable rainImage = getResources().getDrawable(R.drawable.rain);
   
    if(weather.get(position).desc.equals("맑음")){
     conditionImage.setImageDrawable(sunnyImage);
    } else  if(weather.get(position).desc.equals("비")){
     conditionImage.setImageDrawable(rainImage);
    } if(weather.get(position).desc.equals("구름약간")||weather.get(position).desc.equals("구름많음")){
     conditionImage.setImageDrawable(cloudImage);
    }
  
    return customcell;
  }
    }
}


반응형