더 깔끔하게 만들면 좋지만.. 이렇식으로 하니깐 되네요... ^^
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
" <node>"+
" <item>"+
" <checkio>수입</checkio>"+
" <money>2000</money>"+
" </item>"+
" <item>"+
" <checkio>지출</checkio>"+
" <money>-1000</money>"+
" </item></node> ";
ArrayList<Item> itemList = getXmlDataList(xml);
Log.v("TAG", "itemList,size : " + itemList.size());
for (Item item : itemList) {
Log.v("TAG", "item : getCheckio = " + item.getCheckio() + "\t getMoney = " + item.getMoney());
}
private ArrayList<Item> getXmlDataList(String xmlStr) {
ArrayList<Item> ret = new ArrayList<Item>();
Item item = null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader (xmlStr));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {
Log.v("TAG", "Start tag "+xpp.getName());
if (xpp.getName().equals("item")) {
// 아이템
item = new Item();
ret.add(item);
} else if (xpp.getName().equals("checkio")) {
// 수입/지출 구분
if(xpp.next() == XmlPullParser.TEXT) {
item.setCheckio(xpp.getText());
Log.v("TAG", "Text checkio : "+xpp.getText());
}
} else if (xpp.getName().equals("money")) {
// 수입/지출 금액
if(xpp.next() == XmlPullParser.TEXT) {
item.setMoney(xpp.getText());
Log.v("TAG", "Text money : "+xpp.getText());
}
} else {
Log.v("TAG", "Text "+xpp.getText());
}
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
Log.v("TAG", "XmlPullParserException : " + e);
} catch (IOException e) {
Log.v("TAG", "IOException : " + e);
}
return ret;
}
class Item {
private String checkio;
private String money;
public String getCheckio() {
return checkio;
}
public void setCheckio(String checkio) {
this.checkio = checkio;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}