public void getInfo(View v){ //确定要查询的名字 String name = studentName.getText().toString().trim(); if(TextUtils.isEmpty(name)) { Toast.makeText(this, "用户名不能为空", 1); return; } try { XmlPullParser newPullParser = Xml.newPullParser(); File file = new File(this.getFilesDir(), name+".xml"); InputStreamReader in = new InputStreamReader(new FileInputStream(file)); newPullParser.setInput(in); int eventType = newPullParser.getEventType(); String info=""; while(eventType != newPullParser.END_DOCUMENT){ if(eventType == newPullParser.START_TAG) { info+=newPullParser.getName(); info+=":"; }else if(eventType == newPullParser.TEXT) { info+=newPullParser.getText(); info+=" "; } eventType = newPullParser.next(); } studentInfo.setText(info); } catch (Exception e) { // TODO Auto-generated catch block Toast.makeText(this, "没有用户信息", 1).show(); e.printStackTrace(); } }
0则评论给“XmlPullParser解析XML文件”