从sd卡中取出并解析xml,或从assets文件夹下取出并解析xml

把文件从assets文件夹下给取出来并解析的方法:

 

Intent intent;
		String link;
		
		//解析xml
		intent = this.getIntent();
		link = intent.getStringExtra("link");
		InputStreamReader inputReader; 
		SAXBuilder builder = new SAXBuilder();
		
		
		try {
			inputReader = new InputStreamReader(getResources().getAssets().open(link));
			Document doc = builder.build(inputReader);
			Element root = doc.getRootElement();
			// 以下这段程序 是取出item的
			Element elementItemTitle = root.getChild("title");  //标题

		}  catch(Exception e){
			e.printStackTrace();
		}

 

 

===============================================

下面是从sd卡中获得并解析出xml的方法,个人总结

//解析xml
		Intent intent=this.getIntent();
		String link = intent.getStringExtra("link");
		File file = new File(link);
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(file);
			Element root = doc.getRootElement();
			// 以下这段程序 是取出item的
			Element elementItemTitle = root.getChild("title");  //标题

		}  catch (IOException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		}

 

 

你可能感兴趣的:(assets)