¾Èµå·ÎÀ̵忡¼´Â xml °ü·Ã ¼ö´Üµé·Î SAX¿Í DOM ±×¸®°í XmlPullParser¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
XmlPullParser´Â SAXó·³ XML¹®¼¸¦ ÀÐ¾î ³ª°¡¸é¼ ÅÂ±×¿Í ÅؽºÆ®¸¦ ±×¶§±×¶§ ó¸®ÇÑ´Ù.
±×·¯³ª SAXó·³ ó¸®ºÎ Ŭ·¡½º¿Í ÄÝ¹é ¸Þ¼µåµéÀ» ±¸ÇöÇØ¾ß ÇÏ´Â °ÍÀº ¾Æ´Ï´Ù.
Äڵ忡¼´Â ÀûÀýÇÑ ¸Þ¼µå¸¦ ·çÇÁ·Î ¹Ýº¹ È£ÃâÇÏ¸é¼ °¢ »ç°ÇÀ» ó¸®ÇÏ¸é µÈ´Ù.
XMLPulParserÀÇ next() / getEventType() °¡ µ¹·ÁÁÖ´Â »ç°ÇµéÀº ´ÙÀ½°ú °°´Ù.
1.START_TAG : ¿ä¼ÒÀÇ ½ÃÀÛ ÅÂ±× ( ex: <tag> )¸¦ ¸¸³µÀ» ¶§ ÀÌ »ç°ÇÀÌ ¹ÝȯµÈ´Ù.
2.TEXT: ¿ä¼ÒÀÇ ÅؽºÆ®(ex:<tag>text</tag>ÀÇ text)¸¦ ¸¸³µÀ» ¶§ ÀÌ »ç°ÇÀÌ ¹ÝȯµÈ´Ù.
3.END_TAG: ¿ä¼ÒÀÇ Á¾·á ű×(ex:</tag>)¸¦ ¸¸³µÀ» ¶§ ÀÌ »ç°ÇÀÌ ¹ÝȯµÈ´Ù.
4.END_DOCUMENT: XML ÆÄÀÏÀÇ ³¡¿¡ µµ´ÞÇßÀ» ¶§ ÀÌ »ç°ÇÀÌ ¹ÝȯµÈ´Ù.
XmlPullParser¸¦ ÀÌ¿ëÇؼ Á¦ ºí·Î±×ÀÇ rssÇǵå Áß titleÀ» ºÒ·¯¿À´Â ¿¹Á¦ÀÔ´Ï´Ù.
******* ²À uses-permission Ãß°¡ÇØÁÖ¼¼¿ä ********
<uses-permission android:name="android.permission.INTERNET"/>
package com.iyearbook;
import java.io.InputStream;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
URL text = new URL( "http://ibookapp.tistory.com/rss" );
XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserCreator.newPullParser();
parser.setInput( text.openStream(), null );
TextView status = (TextView) findViewById( R.id.status );
status.setText("ÆÄ½Ì Áß..");
int parserEvent = parser.getEventType();
String tag;
boolean inTitle = false;
while (parserEvent != XmlPullParser.END_DOCUMENT ){
switch(parserEvent){
case XmlPullParser.TEXT:
tag = parser.getName();
if (inTitle) {
//if (tag.compareTo("title") == 0) {
Log.i("dd", "title = " + parser.getText() );
//}
}
break;
case XmlPullParser.END_TAG:
tag = parser.getName();
if (tag.compareTo("title") == 0) {
inTitle = false;
}
break;
case XmlPullParser.START_TAG:
tag = parser.getName();
if (tag.compareTo("title") == 0) {
inTitle = true;
}
break;
}
parserEvent = parser.next();
}
status.setText("ÆÄ½Ì ³¡");
}catch( Exception e ){
Log.e("dd", "Error in network call", e);
}
}
}
Ãâó : http://ibookapp.tistory.com/6
* ±ÛÀ» µî·ÏÇϽǶ§ ²À ÇÊ¿äÇÑ °æ¿ì°¡ ¾Æ´Ï¸é °³ÀÎÁ¤º¸¸¦ ³²±âÁö ¸¶¼¼¿ä ^^ (¿¬¶ôó,À̸ÞÀÏÁÖ¼Ò,Áֹεî·Ï¹øÈ£ µî)