기상청 데이터 파싱 예
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;
}
}
}
'Scrapbook > 개발 및 프로그래밍' 카테고리의 다른 글
WebView와 App간 통신하기 (0) | 2011.01.14 |
---|---|
안드로이드에서 Thread 돌리기 (0) | 2011.01.14 |
서버에서 이미지 불러오기 (0) | 2011.01.13 |
Spinner 사용하기 (0) | 2011.01.12 |
[Eclipse] Outline View 아이콘의 의미 (0) | 2011.01.07 |