[Android] XmlPullParser로 xml 파싱하기
게임 개발 초보자/안드로이드 연습장2018. 6. 15. 11:26
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
public class Food {
private String name;
private String price;
private String description;
private String calories; //DTO
public class MainActivity extends AppCompatActivity {
List<Food> foodList;
List<String> list = new ArrayList<>();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
foodList = parseXML();
for (Food food : foodList) {
list.add(food.getName() + "(" + food.getPrice() + ")");
}
ArrayAdapter<String > adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),foodList.get(position).toString(),Toast.LENGTH_SHORT).show();
}
});
}
private List<Food> parseXML() {
List<Food> foods = new ArrayList<>();
boolean isName = false, isDesc = false, isCalories = false, isPrice = false;
String name, desc, price, calories, tagName;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(getResources().openRawResource(R.raw.food), "UTF-8");
Food food = null;
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
tagName = parser.getName();
switch (tagName) {
case "food":
food = new Food();
break;
case "name" :
isName = true;
break;
case "description" :
isDesc = true;
break;
case "price" :
isPrice = true;
break;
case "calories" :
isCalories = true;
break;
}
break;
case XmlPullParser.END_TAG:
tagName = parser.getName();
switch (tagName) {
case "food":
foods.add(food);
break;
case "name" :
isName = false;
break;
case "description" :
isDesc = false;
break;
case "price" :
isPrice = false;
break;
case "calories" :
isCalories = false;
break;
}
break;
case XmlPullParser.TEXT:
if (isName) food.setName(parser.getText());
if (isDesc) food.setDescription(parser.getText());
if (isPrice) food.setPrice(parser.getText());
if (isCalories) food.setCalories(parser.getText());
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("읽은 내용" , foods.toString());
return foods;
}
}
'게임 개발 초보자 > 안드로이드 연습장' 카테고리의 다른 글
[Android] simpleframework로 xml 파일 읽고쓰기 (0) | 2018.06.15 |
---|---|
[Android] Jsoup으로 xml 파싱하기 (프로젝트 안에 있는 파일) (0) | 2018.06.15 |
[Android] id없이 눌린 버튼 사용하기 (0) | 2018.06.12 |
[Android] 파일 저장&불러오기 (0) | 2018.06.07 |
[Android] Adapter extends BaseAdapter 어뎁터 만들기 (0) | 2018.06.07 |