http://www.iteye.com/topic/1122056
android开发过程会经常使用XML文件,下面提供小实例,看看XmlResourceParser类是怎么样解析XML文件的。
XML文件名称为test.xml。内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <customer name="luopeng" age="21" gender="1" emial="dylankeepmoving@163.com"/>
- <customer name="dylan" age="22" gender="2" emial="710097663@qq.com"/>
- <customer name="android" age="6" gender="2" emial="android@gmail.com"/>
- </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<customer name="luopeng" age="21" gender="1" emial="dylankeepmoving@163.com"/>
<customer name="dylan" age="22" gender="2" emial="710097663@qq.com"/>
<customer name="android" age="6" gender="2" emial="android@gmail.com"/>
</resources>
布局文件main.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/textview01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/btn01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn01"
- />
- <EditText
- android:id="@+id/edittext01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/eidttext01"
- />
- </LinearLayout>
<?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/textview01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn01"
/>
<EditText
android:id="@+id/edittext01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eidttext01"
/>
</LinearLayout>
如下则是具体解析XML文件的类TestXmlResourceParserActivity.java:
- package com.dylan.xmlresourceparser;
- 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.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- /*
- * 此类的作用是点击按钮后在EditView上显示资源文件(一个XML)中的内容
- * 此例中会使用到XMLResourceParser
- */
- public class TestXmlResourceParserActivity extends Activity {
- /** Called when the activity is first created. */
- private EditText et;
- private Button myButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 变量在内部类中使用的前提是此变量必须是final修饰的
- myButton = (Button) this.findViewById(R.id.btn01);
- et = (EditText) this.findViewById(R.id.edittext01);
- myButton.setOnClickListener(new OnClickListener() {
- StringBuilder sb = new StringBuilder("");
- Resources res = getResources();
- XmlResourceParser xrp = res.getXml(R.xml.test);
- @Override
- public void onClick(View v) {
- int counter = 0;
- try {
- // 判断是否到了文件的结尾
- while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
- //文件的内容的起始标签开始,注意这里的起始标签是test.xml文件里面<resources>标签下面的第一个标签
- if (xrp.getEventType() == XmlResourceParser.START_TAG) {
- String tagname = xrp.getName();
- if (tagname.endsWith("customer")) {
- counter++;
- 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什么作用呢?
- else if (xrp.getEventType() == XmlResourceParser.END_TAG) {
- } else if (xrp.getEventType() == XmlResourceParser.TEXT) {
- }
- xrp.next();
- }
- //程序细节注意的地方,StringBuilder要条用toString()方法,不要粗心忘记了
- et.setText(sb.toString());
- } catch (XmlPullParserException e) {
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
package com.dylan.xmlresourceparser;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/*
* 此类的作用是点击按钮后在EditView上显示资源文件(一个XML)中的内容
* 此例中会使用到XMLResourceParser
*/
public class TestXmlResourceParserActivity extends Activity {
/** Called when the activity is first created. */
private EditText et;
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 变量在内部类中使用的前提是此变量必须是final修饰的
myButton = (Button) this.findViewById(R.id.btn01);
et = (EditText) this.findViewById(R.id.edittext01);
myButton.setOnClickListener(new OnClickListener() {
StringBuilder sb = new StringBuilder("");
Resources res = getResources();
XmlResourceParser xrp = res.getXml(R.xml.test);
@Override
public void onClick(View v) {
int counter = 0;
try {
// 判断是否到了文件的结尾
while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
//文件的内容的起始标签开始,注意这里的起始标签是test.xml文件里面<resources>标签下面的第一个标签
if (xrp.getEventType() == XmlResourceParser.START_TAG) {
String tagname = xrp.getName();
if (tagname.endsWith("customer")) {
counter++;
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什么作用呢?
else if (xrp.getEventType() == XmlResourceParser.END_TAG) {
} else if (xrp.getEventType() == XmlResourceParser.TEXT) {
}
xrp.next();
}
//程序细节注意的地方,StringBuilder要条用toString()方法,不要粗心忘记了
et.setText(sb.toString());
} catch (XmlPullParserException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
运行结果如下图: