XmlPullParser解析XML文件

	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();
		}
	}
	

原文链接: XmlPullParser解析XML文件 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-112.html )

发表评论

0则评论给“XmlPullParser解析XML文件”