Android读取XML资源 分类: Android 2015-07-27 16:40 10人阅读 评论(0) 收藏

获取XML文件的基本思路是,通过getResources().getXml()获的XML原始文件,得到XmlResourceParser对象,通过该对象来判断是文档的开头还是结尾,是某个标签的开始还是结尾,并通过一些获取属性的方法来遍历XML文件,从而访问XML文件的内容,下面是一个访问XML文件内容的例子,并将内容更显示在一个TextView上

 

ReadXMLTest.java

package org.hualang.readxml;   
  
import java.io.IOException;   
import org.xmlpull.v1.XmlPullParser;   
import org.xmlpull.v1.XmlPullParserException;   
  
import android.app.Activity;   
import android.content.res.Resources;   
import android.content.res.XmlResourceParser;   
import android.os.Bundle;   
import android.view.View;   
import android.view.View.OnClickListener;   
import android.widget.Button;   
import android.widget.TextView;   
import org.hualang.readxml.R;   
  
public class ReadXMLTest extends Activity {   
    private TextView myTextView;   
    private Button myButton;   
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
       setContentView(R.layout.main);   
          
       myTextView = (TextView)findViewById(R.id.text);   
       myButton = (Button)findViewById(R.id.button);   
       //设置按钮监听器   
       myButton.setOnClickListener(new OnClickListener() {   
        @Override  
        public void onClick(View v) {   
            //设置定时器   
               int counter = 0;   
               //实例化StringBuilder   
               StringBuilder sb = new StringBuilder("");   
               //得到Resources资源   
               Resources r = getResources();   
               //通过Resources,获得XmlResourceParser实例   
               XmlResourceParser xrp = r.getXml(R.xml.test);   
               try {   
                   //如果没有到文件尾继续执行   
                while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {    
                    //如果是开始标签   
                         if (xrp.getEventType() == XmlResourceParser.START_TAG) {   
                             //获取标签名称   
                              String name = xrp.getName();   
                              //判断标签名称是否等于friend   
                              if(name.equals("friend")){   
                                  counter++;   
                                  //获得标签属性追加到StringBuilder中   
                                  sb.append("第"+counter+"个朋友的信息:"+"\n");   
                                  sb.append(xrp.getAttributeValue(0)+"\n");   
                                  sb.append(xrp.getAttributeValue(1)+"\n");   
                                  sb.append(xrp.getAttributeValue(2)+"\n");   
                                  sb.append(xrp.getAttributeValue(3)+"\n\n");   
                              }   
                         } else if (xrp.getEventType() == XmlPullParser.END_TAG) {    
                         } else if (xrp.getEventType() == XmlPullParser.TEXT) {    
                         }    
                         //下一个标签   
                         xrp.next();    
                    }   
                myTextView.setText(sb.toString());   
            } catch (XmlPullParserException e) {   
                e.printStackTrace();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
    });   
    }   
}  

package org.hualang.readxml;  
  
import java.io.IOException;  
import org.xmlpull.v1.XmlPullParser;  
import org.xmlpull.v1.XmlPullParserException;  
  
import android.app.Activity;  
import android.content.res.Resources;  
import android.content.res.XmlResourceParser;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
import org.hualang.readxml.R;  
  
public class ReadXMLTest extends Activity {  
    private TextView myTextView;  
    private Button myButton;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
       setContentView(R.layout.main);  
         
       myTextView = (TextView)findViewById(R.id.text);  
       myButton = (Button)findViewById(R.id.button);  
       //设置按钮监听器  
       myButton.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View v) {  
            //设置定时器  
               int counter = 0;  
               //实例化StringBuilder  
               StringBuilder sb = new StringBuilder("");  
               //得到Resources资源  
               Resources r = getResources();  
               //通过Resources,获得XmlResourceParser实例  
               XmlResourceParser xrp = r.getXml(R.xml.test);  
               try {  
                   //如果没有到文件尾继续执行  
                while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {   
                    //如果是开始标签  
                         if (xrp.getEventType() == XmlResourceParser.START_TAG) {  
                             //获取标签名称  
                              String name = xrp.getName();  
                              //判断标签名称是否等于friend  
                              if(name.equals("friend")){  
                                  counter++;  
                                  //获得标签属性追加到StringBuilder中  
                                  sb.append("第"+counter+"个朋友的信息:"+"\n");  
                                  sb.append(xrp.getAttributeValue(0)+"\n");  
                                  sb.append(xrp.getAttributeValue(1)+"\n");  
                                  sb.append(xrp.getAttributeValue(2)+"\n");  
                                  sb.append(xrp.getAttributeValue(3)+"\n\n");  
                              }  
                         } else if (xrp.getEventType() == XmlPullParser.END_TAG) {   
                         } else if (xrp.getEventType() == XmlPullParser.TEXT) {   
                         }   
                         //下一个标签  
                         xrp.next();   
                    }  
                myTextView.setText(sb.toString());  
            } catch (XmlPullParserException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    });  
    }  
}  

main.xml

<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >   
<Button   
    android:id="@+id/button"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="读取XML文件内容"  
/>   
<TextView     
    android:id="@+id/text"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"    
    android:text=""  
    />   
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<Button  
    android:id="@+id/button"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="读取XML文件内容"  
/>  
<TextView    
    android:id="@+id/text"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"   
    android:text=""  
    />  
</LinearLayout> 

res/xml目录下的test.xml文件

<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <friend name="hualang" age="22" gender="male" email="hualang@yahoo.com"/>   
    <friend name="nimo" age="21" gender="female" email="1234567@qq.com"/>   
</resources>  
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <friend name="hualang" age="22" gender="male" email="hualang@yahoo.com"/>  
    <friend name="nimo" age="21" gender="female" email="1234567@qq.com"/>  
</resources>  

运行结果

注意:如果没有加入import org.hualang.readxml.R;这句话,点击按钮后没有反应,原因不知道


加入import org.hualang.readxml.R;后,运行结果如下
 



posted @ 2015-07-27 16:40  xiexie2015  阅读(123)  评论(0编辑  收藏  举报