如何读取本地xml文件是资源文件夹作为android中的输入流?

时间:2022-05-04 00:35:27

I am trying to get input stream from something like this.

我试图从这样的东西获取输入流。

InputSource myInputSource = new InputSource(activity.getResources().openRawResource(com.MYCLass.R.xml.programs));

myXMLReader.parse(myInputSource);

and then call parse on the parser instance i Created. Some how i get nothing.
Works fine if I use a server XML....

然后在我创建的解析器实例上调用解析。有些我什么都没得到。如果我使用服务器XML,工作正常....

2 个解决方案

#1


11  

Put the xml file into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you an instance of XML parser.

将xml文件放入/ res / raw文件夹。看起来openRawResource只打开该文件夹中的资源。您也可以尝试getResources()。getXml(com.MYCLass.R.xml.programs);这将返回一个XML解析器的实例。

A piece of code taken from @Krupa

一段代码取自@Krupa

InputStream object = this.getResources()
                     .openRawResource(R.raw.fileName);

#2


4  

You can read the file by the following code :

您可以通过以下代码读取该文件:

InputStream object = this.getResources()
        .openRawResource(R.rawFolderName.fileName);

#1


11  

Put the xml file into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you an instance of XML parser.

将xml文件放入/ res / raw文件夹。看起来openRawResource只打开该文件夹中的资源。您也可以尝试getResources()。getXml(com.MYCLass.R.xml.programs);这将返回一个XML解析器的实例。

A piece of code taken from @Krupa

一段代码取自@Krupa

InputStream object = this.getResources()
                     .openRawResource(R.raw.fileName);

#2


4  

You can read the file by the following code :

您可以通过以下代码读取该文件:

InputStream object = this.getResources()
        .openRawResource(R.rawFolderName.fileName);