Android--XML的讀取,使用XmlPullParser

=================================
test.xml
=================================

<? xml version= "1.0" encoding= "utf-8" ?>
    <channel >
        <title >旅遊 </title >
        <description >(...略) </description >
        <language >zh-Hant-TW </language >
        <pubDate >Sun, 06 Jan 2013 </pubDate >
        <ttl >5 </ttl >
        <item >
            <title >台南美食吃不完 </title >
               <pubDate >Sun, 06 Jan 2013 </pubDate >
                <description >(...略) </description >
        </item >
    </channel >



====================================
activity_main.xml
====================================
< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
    xmlns:tools= "http://schemas.android.com/tools"
    android:id= "@+id/RelativeLayout1"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent"
    android:paddingBottom= "@dimen/activity_vertical_margin"
    android:paddingLeft= "@dimen/activity_horizontal_margin"
    android:paddingRight= "@dimen/activity_horizontal_margin"
    android:paddingTop= "@dimen/activity_vertical_margin"
    tools:context= ".MainActivity" >

    <FrameLayout
        android:id= "@+id/frameLayout1"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_alignParentLeft ="true"
        android:layout_alignParentTop ="true" >

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

    </FrameLayout >

    <FrameLayout
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_alignParentRight ="true"
        android:layout_alignTop= "@+id/frameLayout1"
        android:layout_marginRight= "123dp" >

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

    </FrameLayout >

</ RelativeLayout>



===================================
MainActivity.java
===================================
package com.example.test_app06;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Xml;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          TextView tv01 = (TextView)findViewById(R.id.textView1);
          TextView tv02 = (TextView)findViewById(R.id.textView2);
          int i= 0;
          XmlPullParser pullParser = Xml.newPullParser();
          //因為將test.xml放在/assets之下,所以必須以讀取檔案的方式來處理
          AssetManager assetManager = getAssets();
          InputStream is;
          try
          {
              is = assetManager.open("test.xml"); //讀取檔案
              pullParser.setInput(is , "utf-8");  //設定語系
              //利用eventType來判斷目前分析到XML是哪一個部份
              int eventType = pullParser.getEventType();
              //XmlPullParser.END_DOCUMENT表示已經完成分析XML
              while(eventType != XmlPullParser.END_DOCUMENT)
              {
                  i++;
                  //XmlPullParser.START_TAG表示目前分析到的是XML的Tag,如<title>
                  if (eventType == XmlPullParser.START_TAG) {
                    String name = pullParser.getName();
                    tv01.setText(tv01.getText() + ", " + name);
                  }
                  //XmlPullParser.TEXT表示目前分析到的是XML Tag的值,如:台南美食吃不完
                  if (eventType==XmlPullParser.TEXT) {
                       String value = pullParser.getText();
                      tv02.setText(tv02.getText() + ", " + value);
                  }
                  //分析下一個XML Tag
                  eventType = pullParser.next();
              }
          } catch (IOException e) {
               tv02.setText(e.toString());
          } catch (XmlPullParserException e) {
               tv02.setText(e.toString());
          }
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
     }

}

Oracle Data Gurad Archive Gap