原始xml资源的使用

如上,可以对原始xml进行访问

下面我介绍自己试验过的一个例子:

1.首先定义一个原始xml文件,放在res/xml中的test.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<customer name="tom" age="20" gender="male" email="liuxiao535@gmail.com"></customer>
<customer name="liuxiao" age="19" gender="female" email="xinxiaxiao@163.com"></customer>
</resources>

放在<resources></resources>之间的资源文件都可以在R类中自动生成定义标示,以后可以用R.xml.xml_name引用

2.然后进行布局文件的编写:

<?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" >
<Button
android:text="获得xml内容"
android:id="@+id/xmltTestButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:text=""
android:id="@+id/xmlContentTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
</LinearLayout>

在这里,添加了两个控件,进行了相应的布局,并且把相应的id加入R中,用以此后的使用

3.最后在activity的继承类中进行编写相应的操作代码,包括button的点击响应代码

package com.example.liuxiao;
import android.annotation.SuppressLint;

/*

Lint是一个静态检查器,它围绕Android项目的正确性、安全性、性能、可用性以及可访问性进行分析。它检查的对象包括XML资源、位图、ProGuard配置文件、源文件甚至编译后的字节码。

这一版本的Lint包含了API版本检查性能检查以及其他诸多特性。其中还有一个重要的改动是Lint可以使用@SuppressLint标注忽略指定的警告。

*/
import android.app.Activity;
import android.content.res.Resources;
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.view.View;
import android.view.View.OnClickListener;

import android.os.Bundle;
import android.widget.TextView;
import com.example.liuxiao.R;
import android.widget.Button;
public class TestColorActivity extends Activity
{
private TextView myTextView;
private Button myButton;
@SuppressLint({ "ParserError", "ParserError" })
@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.test_xml);//自动引用文件夹的名导R类中
myButton=(Button)findViewById(R.id.xmltTestButton01);
myTextView=(TextView)findViewById(R.id.xmlContentTextView01);
//设置按钮的单击事件监听器
myButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int counter=0;
StringBuilder sb=new StringBuilder("");
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();
if(name.equals("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(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();}
}
});

}
}

最后的运行结果如图:

原文地址:https://www.cnblogs.com/yehai/p/2613231.html