main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
item.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:tag="hi"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:background="#99dd99"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
test.xml<?xml version="1.0" encoding="UTF-8"?>
<test>
<item>
<name>치킨</name>
<price>15000</price>
<location>묵동</location>
<market>페리카나</market>
<image></image>
</item>
<item>
<name>토스트</name>
<price>1600</price>
<location>묵동</location>
<market>이삭토스트</market>
<image>http://png-2.findicons.com/files//icons/2010/free_food/128/bread1.png</image>
</item>
<item>
<name>피자</name>
<price>8000</price>
<location>묵동</location>
<market>피자스쿨</market>
<image>http://png-5.findicons.com/files//icons/342/food/128/pizza_slice.png</image>
</item>
</test>
manifast.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xmlparserexercise"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PullParserActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
PullParserActivity.xmlpackage com.xmlparserexercise;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class PullParserActivity extends Activity{
ListView listView;
//Item을 담을 Collection
ArrayList<Item> list=new ArrayList<Item>();
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.main);
listView=(ListView) findViewById(R.id.listView1);
// res/xml/test.xml를 읽는 parser호출
XmlResourceParser parser =getResources().getXml(R.xml.test);
try{
int eventType =parser.getEventType();
/* START_DOCUMENT = 0
* END_DOCUMENT = 1
* START_TAG = 2
* END_TAG = 3
* TEXT = 4*/
String name="";
String price="";
String image="";
String tag="";
String text="";
boolean isEnd=true;
while(isEnd){
switch(eventType){
case 0: //START_DOCUMENT = 0
Log.e("START_DOCUMENT","START_DOCUMENT"+parser.getName());
break;
case 1: //END_DOCUMENT = 1
Log.e("END_DOCUMENT","END_DOCUMENT"+parser.getName());
isEnd=false;
break;
case 2: //START_TAG = 2
Log.i("START_TAG","START_TAG"+parser.getName());
break;
case 3: //END_TAG = 3
Log.w("END_TAG","END_TAG"+parser.getName());
//....떡볶이</
if(parser.getName().equals("name")){
name=text;
//2000</price>
}else if(parser.getName().equals("price")){
price=text;//name="떡볶이" price="2000" 완성됨
Item item = new Item(name,price);//객체 생성
}else if(parser.getName().equals("image")){
image=text;
Item item = new Item(name,price,image);
list.add(item);
}
break;
case 4: //TEXT = 4
Log.d("TEXT","TEXT"+parser.getName());
text=parser.getText();
break;
}
eventType=parser.next();//다음으로 이동
}//while문 끝
MyAdapter adapter= new MyAdapter();
listView.setAdapter(adapter);
}catch(Exception e){
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
//return list.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
/* item.xml을 불러와서 이 안에 있는 TextView에
* Collection안에 들은 Item객체의 값을 셋팅하고
* Item.xml을 retrun하기*/
LinearLayout layout=
(LinearLayout)View.inflate(getApplicationContext(), R.layout.item, null);
Item oneItem = list.get(position);
//layout에서 textView를 찾아서 findViewById
//oneItem의 필드값으로 셋팅하기
TextView tv1 = (TextView)layout.findViewById(R.id.textView1);
TextView tv2 = (TextView)layout.findViewWithTag("hi");
ImageView imgV=(ImageView)layout.findViewById(R.id.imageView1);
tv1.setText(oneItem.name);
tv2.setText(oneItem.price);
if(oneItem.image!=null){
try {
URL url=new URL(oneItem.image);
try {
InputStream is=url.openStream();
Bitmap bitmap =BitmapFactory.decodeStream(is);
imgV.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return layout;
}
}
class Item{//innerClass XML의 한단위에 해당
String name="";
String price="";
String image="";
public Item(String n,String p){
name=n;
price=p;
}
//생성자 오버로딩,다양성
public Item(String n,String p,String i){
name=n;
price=p;
image=i;
}
}
}