플로렌스라는 개발자

[안드로이드 프로그래밍]홈페이지(웹)xml 파싱후 출력

프로그래밍/Android 2016.05.22 댓글 0 Plorence

목차

    홈페이지 xml 내용

    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
    <channel>
    <title>xml 파싱하기</title>
    <middletext>성공</middletext>
    <endtext>은근 쉽네요</endtext>
    <lasttext>강좌는 나중에</lasttext>
    </channel>
    </rss>



    JAVA


    import android.app.*;
    import android.os.*;
    import android.os.Build.*;
    import android.view.*;
    import android.widget.*;
    import java.net.*;
    import java.io.*;
    import org.xmlpull.v1.*;

    public class 파일이름 extends Activity
    {

    private String tagName;

    private String result;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xml파일);

    if (VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    }

    try {
    URL url = new URL("http://batorio.dothome.co.kr/ququ.xml");
    InputStream in = url.openStream();
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(in, "utf-8");

    int eventType = parser.getEventType();
    boolean isItemTag = false;

    while (eventType != XmlPullParser.END_DOCUMENT) {

    if (eventType == XmlPullParser.START_TAG) {

    tagName = parser.getName();
    if (tagName.equals("rss"))
    isItemTag = true;


    } else if (eventType == XmlPullParser.TEXT && isItemTag) {

    if(tagName.equals("title")) {

    result = parser.getText();
    TextView resultText = (TextView)findViewById(R.id.title);
    resultText.append(result);
    }
    if(tagName.equals("middletext"))
    {
    result = parser.getText();
    TextView resultText = (TextView)findViewById(R.id.result);
    resultText.append(result);
    }
    if(tagName.equals("endtext"))
    {
    result = parser.getText();
    TextView resultText = (TextView)findViewById(R.id.afterusing);
    resultText.append(result);
    }
    if(tagName.equals("lasttext"))
    {
    result = parser.getText();
    TextView resultText = (TextView)findViewById(R.id.endingsentence);
    resultText.append(result);
    }
    }
    else if (eventType == XmlPullParser.END_TAG)
    {
    tagName = parser.getName();
    if(tagName.equals("rss"))
    {
    isItemTag = false;
    }
    }

    eventType = parser.next();
    }
    } catch(Exception e) {
    Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG)
    .show();
    }

    }
    }


    메니페스트

    <uses-permission android:name="android.permission.INTERNET" />

    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" >

    <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="title:" />

    <TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="결과:" />

    <TextView
    android:id="@+id/afterusing"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="후기:" />

    <TextView
    android:id="@+id/endingsentence"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="마무리말:" />

    </LinearLayout>


    출처:http://cafe.naver.com/orangef914k/22062

    댓글

    0