test.xml
<?xml version="1.0" encoding="UTF-8"?> <test> <!-- data: 하루의 날씨 정보를 갖는다. --> <data> <day>0</day> <high>26</high> <low>10</low> </data> <data> <day>1</day> <high>30</high> <low>15</low> </data> <data> <day>2</day> <high>36</high> <low>20</low> </data> </test>XMLParserExerciseActivity.java
package com.xmlparserexercise; import android.app.Activity; import android.content.res.Resources; import android.content.res.XmlResourceParser; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class XMLParserExerciseActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res =getResources();// res/xml/test.xml XmlResourceParser parser =res.getXml(R.xml.test); TextView textView=new TextView(this); try { int eventType = parser.getEventType(); StringBuilder sb=new StringBuilder(); while(eventType != XmlResourceParser.END_DOCUMENT){ //<day>만났을 때 if(eventType==XmlResourceParser.START_TAG){ String tagName=parser.getName(); sb.append(tagName); //day를 붙이기 } //<day>2</day> if(eventType==XmlResourceParser.TEXT){ String text=parser.getText(); Log.i("Text", "text= "+text); sb.append(text+"\n"); //2를 붙이기 } eventType = parser.next(); } textView.setText(sb.toString()); setContentView(textView); } catch (Exception e) { } } }
'Android > 2012.04월 강좌' 카테고리의 다른 글
10일차 TabView (0) | 2012.05.09 |
---|---|
10일차 PullParser (1) | 2012.05.09 |
10일차 BroadcastReceiver (0) | 2012.05.09 |
10일차 Alarm (0) | 2012.05.09 |
9일차 ViewFlipper (0) | 2012.05.08 |