external xml file integration in android - android

i am developing one application for displaying images and labels and its sound.i want to get this info from xml .where i need to keep xml file in android project how to get values from that xml file
Thanks in advance
Aswan

You can package the xml file in assets/ directory inside your apk. With that, you can use
AssetManager assets = context.getAssets();
InputStream in = assets.open("myfile.xml");
If your file just contains labels, or strings, you can save it in put it in res/values/strings.xml inside your apk. In this case you can access the string values using the android generated R class
If you have a custom file (with custom structure) you can use DOM, SAX, or Xml Pull to read those files. e.g.
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);

Related

Reading JSON or text file in resource folder

I am developing Kotlin app.
I am trying to read text and JSON files which I stored in resource folder(res/raw/file.txt).
Looking at the hierachy of app structure, I put absolute path(../../res/raw/file.txt) for file reader.
Unfortunately, my app cannot find the file.
Can you tell me how I can read files stored in res/raw? If I store file in wrong directory, can you tell me where to store and how to access them?
Thanks
If you want to read a file from res/raw folder you can obtain InputStream by R.raw.yourfile id like this:
resources.openRawResource(R.raw.yourfile)
Or you can open file by Uri:
val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile")
val file = File(uri.getPath());
Alternatevily you can put your files in assets/ folder and get InputStream like this:
assets.open("yourfile.txt")

How to save many story in assert in android

I am creating an application where data are saved in .txt file in assets folder, I am trying to retrieve my data but I am not able to do, My teacher conditions to not to use SQLite and shared preference.
Place your text file in the /assets directory under the Android project. Use AssetManager class to access it.
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
Or you can also put the file in the /res/raw directory, where the file will be indexed and is accessible by an id in the R file:
InputStream is = getResources().openRawResource(R.raw.test);

Res->Raw , File path i use "res/raw/filename.xml" but doesn't work

I have an XML File saved in Res->Raw , i want to get the path of this file to passed it in argument of function to parse it.
The name of XML file is families.xml , so I try this but doesn't work : "res/raw/families.xml". Any help?
Use :
InputStream is=getResources().openRawResource(R.raw.families);
to get an InputStream to your XML, and then use XMLPullParser to parse the file :
XmlPullParser xpp=XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(is,"your-encoding");
Another alternative would be to put it in the assets folder.
res/ is just source folder. It does not exist in final app. If you want to retrieve your file you may want to either put it in assets folder or use getResources().openRawResource()

How to access local xml file from resource in Android?

I am new to Android application, create one xml file data.xml inside the Resource folder, then i tried to access that local xml file, but i can't get it.
But android resource folder and open rawresource to the raw folder under res inside xml file
data.xml using this way, i access that xml file like:
InputStream is = this.getResources().openRawResource(R.raw.data);
but using local xml file cannot access, please help me.
Thanks
raw xmls should be in Assets folder, and to access files from assets folder use:
AssetManager assetManager = getAssets();
InputStream in= assetManager.open("data.xml");
Add your data.xml file under raw folder.After inserting into it you will not get this sort of error.

Where to place an XML file containing data within an Android app

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?
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:
res
No subdirectorys are allowed under
the specific resource-folders.
The R-class indexes all resources
and provides simple access.
There are some simple methods which
help reading files stored in the
res-directory
assets
Subdirectorys are allowed (as much as
you like).
No indexing by the R-class
Reading resources stored in assets
is done using the AssetManager.
You can put it in the res/raw folder. Then you will access it using:
getResources().openRawResource(resourceName)
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.

Categories

Resources