80,351
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<resources>
<customer name="tom" age="20" gender="male" email="tom@yahoo.com"/>
<customer name="kite" age="21" gender="male" email="kite@yahoo.com"/>
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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:text="@string/test_xml"
android:id="@+id/xmlTestButton01"
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"/>
</RelativeLayout>
package com.amaker.ch03.xml;
import com.amaker.ch03.xml.R;
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;
public class TextXmlActivity extends Activity {
private TextView myTextView;
private Button myButton;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_xml);
myTextView=(TextView)findViewById(R.id.xmlContentTextview01);
myButton=(Button)findViewById(R.id.xmlTestButton01);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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_DOCUMENT){
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");
}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();
}
}
});
}
}