I am new to Android development. I have an XML file with data that the app will read. Where should I keep this XML file? Should it be stored within the \"value\" folder?<
You can put it in the res/raw folder. Then you will access it using:
getResources().openRawResource(resourceName)
I'd say that depends. What do you save in your XML-File? There also is a res/xml
-folder, where XML-Files can be kept. But Android does nearly anything with XML-Files, so you might want to read my little Tutorial about where to put which recourses.
Also, there is a difference between the assets
and the res
-directory's:
R
-class indexes all resources
and provides simple access.res
-directoryR
-classassets
is done using the AssetManager.I had a similar requirement and after lot of research , I found 2 solution to place a custom XML : You can place custom XML in
res/raw/
res/xml/
To access these location you will use following code :
a. if XML is placed in res/raw then :
getResources().openRawResource(R.raw.custom-xml) :
This gives you easy methods for reading xml :
with below code I am reading XML in memory placed in raw folder :
BufferedReader br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.custom-xml)));
StringBuilder str = new StringBuilder();
String line;
while ( (line = br.readLine()) != null){
str.append(line);
}
2nd Option :
getResources().getxml(R.xml.custom-xml);
with this you could read the xml using eventbased parser.