开发者

How to read an xml file from R.java

开发者 https://www.devze.com 2023-04-10 05:55 出处:网络
I have an xml file inside the res folder under 开发者_开发问答the xml forlder. I want to parse it with SAX. I know there are lots of example but I want to understand InputStream, InputSource, File dee

I have an xml file inside the res folder under 开发者_开发问答the xml forlder. I want to parse it with SAX. I know there are lots of example but I want to understand InputStream, InputSource, File deeply. I can reach the file writing R.xml.hippo (hippo is the name of the xml file).

But how can I give this resource as a stream? I know when I write R.xml.hippo to anywhere it will be reference(int) but I want give it as a source. What is and how converting an int to starting point of stream?


You can read XML from assets folder as well as from res/raw folder.

To read from assets folder

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

To read from raw folder

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

And parse this inputStream to your XML Parser.


You should place the file inside the raw folder. Then you can access it via Resources.openRawResource(). For more information refer to Android Developer's page.

0

精彩评论

暂无评论...
验证码 换一张
取 消