我对 setInput() 方法有疑问。当我提供普通 StringReader 并解析 XML
locationsXml.setInput(new StringReader("<locations>" + "<location>" etc.[...]
一切顺利,但如果我想从资源中加载文件:
locationsXml.setInput(new FileReader("res/xml/locations.xml"));
文件未加载。
哪里有问题?
我对 setInput() 方法有疑问。当我提供普通 StringReader 并解析 XML
locationsXml.setInput(new StringReader("<locations>" + "<location>" etc.[...]
一切顺利,但如果我想从资源中加载文件:
locationsXml.setInput(new FileReader("res/xml/locations.xml"));
文件未加载。
哪里有问题?
您已经从资源中的文件创建了一个 Inputstream(前提是资源 xml 位于“assets”文件夹中)。您可以使用以下方法控制该资源:
context.getAssets.open(文件名);
例如 - InputStream myInput = myContext.getAssets().open(locations.xml);
现在 - 你可以使用这个 inputStream 来做任何你想做的事情。
或者
将 xml 放入 /res/raw 文件夹。看起来 openRawResource 仅打开该文件夹中的资源。您也可以尝试 getResources().getXml(com.MYCLass.R.xml.programs); 这将返回您的 XML 解析器实例。
InputStream 对象 = this.getResources() .openRawResource(R.raw.fileName);