在前面講到了讀取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; } }