protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_xml);
xmlTxtView=(TextView)findViewById(R.id.TxtView11);
xmlBtn=(Button)findViewById(R.id.button11);
xmlBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int counter=0;
StringBuilder sb=new StringBuilder("");
//获取xml文件
Resources r=getResources();
XmlResourceParser xrp=r.getXml(R.xml.test);
try{
int xrpType=xrp.getEventType();
//如果文件没有结尾继续循环
while(xrpType!=XmlResourceParser.END_DOCUMENT){
//如是开始标签
if(xrpType==XmlResourceParser.START_DOCUMENT){
//这里name为空,没有取到xml里面的值。求解???
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()==XmlResourceParser.END_TAG){
}else if(xrp.getEventType()==XmlResourceParser.TEXT){
}
//下一个
System.out.println("这是啥么子:"+ xrp.nextText());
//xrp.nextText();
}//while
xmlTxtView.setText(sb.toString());
}catch(XmlPullParserException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
});//click
}
}
读取xml时,调试了下,xml中有值,不知道为什么取xrp.getName();却为空?
super.onCreate(savedInstanceState);
setContentView(R.layout.test_xml);
xmlTxtView=(TextView)findViewById(R.id.TxtView11);
xmlBtn=(Button)findViewById(R.id.button11);
xmlBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int counter=0;
StringBuilder sb=new StringBuilder("");
//获取xml文件
Resources r=getResources();
XmlResourceParser xrp=r.getXml(R.xml.test);
try{
int xrpType=xrp.getEventType();
//如果文件没有结尾继续循环
while(xrpType!=XmlResourceParser.END_DOCUMENT){
//如是开始标签
if(xrpType==XmlResourceParser.START_DOCUMENT){
//这里name为空,没有取到xml里面的值。求解???
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()==XmlResourceParser.END_TAG){
}else if(xrp.getEventType()==XmlResourceParser.TEXT){
}
//下一个
System.out.println("这是啥么子:"+ xrp.nextText());
//xrp.nextText();
}//while
xmlTxtView.setText(sb.toString());
}catch(XmlPullParserException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
});//click
}
}
读取xml时,调试了下,xml中有值,不知道为什么取xrp.getName();却为空?