安卓读取XML

在前面讲到了读取txt文件的操做,下面说的是读取xml文件java

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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" >
    <Button 
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/button"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLaeft="@+id/button"
        android:layout_alignRight="@+id/button"
        android:layout_below="@+id/button" />
</RelativeLayout>

XML的位置:res下新建xml文件夹,在里面建立test.xml,若是直接建立在res下,xml会报错android

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <friend name="John" age="24" gender="male" email="John@inspur.com" />
    <friend name="mario" age="25" gender="female" email="mario@inspur.com" />
</resources>

Java代码:app

package com.example.readxml;
import java.io.IOException;
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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.readxml.R;
public class MainActivity extends Activity {
 private TextView myTextView;  
 private Button myButton; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  myTextView=(TextView)findViewById(R.id.text);
  myButton=(Button)findViewById(R.id.button);
  //设置监听
  myButton.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //设置定时器
    int counter=0;
    //实例化StringBuilder
    StringBuilder sb=new StringBuilder("");
    //获得Resource资源
    Resources r=getResources();
    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");
       }
      }else if(xrp.getEventType()==XmlResourceParser.END_TAG){
      }else if(xrp.getEventType()==XmlResourceParser.TEXT){
      }
      xrp.next();
     }
     myTextView.setText(sb.toString());
    } catch (XmlPullParserException e) {
     // TODO: handle exception
     e.printStackTrace();
    }catch (IOException e) {
     // TODO: handle exception
     e.printStackTrace();
    }
   }
  });
 }
 @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;
 }
}
相关文章
相关标签/搜索