如何从R.java读取xml文件

时间:2011-10-02 09:37:20

标签: android xml

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

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

2 个答案:

答案 0 :(得分:2)

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

从资源文件夹中读取

AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");

从原始文件夹中读取

InputStream inputStream = getResources().openRawResource(R.raw.your_xml);

parse inputStream到您的XML解析器。

答案 1 :(得分:1)

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