Android 如何从R.java读取xml文件

Android 如何从R.java读取xml文件,android,xml,Android,Xml,我在xml文件夹下的res文件夹中有一个xml文件。我想用SAX解析它。我知道有很多例子,但我想深入了解InputStream、InputSource和文件。我可以通过编写R.xml.hippo(hippo是xml文件的名称)来访问文件 但我如何将此资源作为流提供? 我知道当我写R.xml.hippo到任何地方时,它都是引用(int),但我想把它作为源代码。什么是以及如何将int转换为流的起点?您应该将文件放在raw文件夹中。然后您可以通过Resources.openRawResource()访

我在xml文件夹下的res文件夹中有一个xml文件。我想用SAX解析它。我知道有很多例子,但我想深入了解InputStream、InputSource和文件。我可以通过编写
R.xml.hippo
(hippo是xml文件的名称)来访问文件

但我如何将此资源作为流提供?
我知道当我写
R.xml.hippo
到任何地方时,它都是引用(int),但我想把它作为源代码。什么是以及如何将int转换为流的起点?

您应该将文件放在
raw
文件夹中。然后您可以通过
Resources.openRawResource()
访问它。有关更多信息,请参阅。

您可以从
assets
文件夹以及
res/raw
文件夹中读取XML

从资产文件夹中读取

AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");
InputStream inputStream = getResources().openRawResource(R.raw.your_xml);
从原始文件夹读取

AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");
InputStream inputStream = getResources().openRawResource(R.raw.your_xml);

parse
将此
inputStream
输入到您的XML解析器。

非常清楚的解释。感谢您提供getAssets(),即使这不是我的问题。但我已经学会了。