如何读取本地XML文件是资源文件夹作为Android的一个输入流?文件夹、文件、资源、XML

2023-09-06 01:16:34 作者:那抹笑下的痛誰知

我想从这样的事情让输入流。

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

,然后调用解析的解析器实例我创建。一些如何,我什么也得不到。 如果我使用一台服务器的XML做工精细....

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

推荐答案

把XML文件放到 / RES /原始文件夹。它看起来像 openRawResource 只从该文件夹打开资源。您也可以尝试 getResources()的getXML(com.MYCLass.R.xml.programs); 这会回报你的XML解析器的一个实例

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.

一块code从@Krupa采取

A piece of code taken from @Krupa

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