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

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.

Related

Android: Writing to XML file in Assets

I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)

Android - iterating through Zipentry

I have large amount of files in a ZIP file (lets say 1000 images, some db files, binary files, ...). Inside it, I have some xml file I need to find and parse it. Information from it is shown to the screen. Problem is, when I am iterating through zip entry using:
InputStream inputStream = new FileInputStream(zipPath);
in = new ZipInputStream(inputStream);
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
...some code here...
}
So when I am using Log.d, I see, it is iterating file by file, in case of large amount of files, it could take several minutes. Is there any better way, how to locate one specific file among others in ZIP file? "Brute force" approach I am using now is time consuming.
Thanks for any ideas
Waypoint
You can probably do this:
BufferedReader in = new BufferedReader(new InputStreamReader(zipfile.getInputStream(entry)));
When extracting specific files, you should be using a ZipFile. In particular, ZipFile.getEntry().
This should be no problem since you are using a File. However, if you only have an InputStream (or you prefer to use ZipInputStream for some reason) then, if you control how the zip file is built, you should put the XML file as the first entry.

CSV File Import/Reading - Android

Pretty short question:
I have put a .csv file in my assets folder. My problem is that I'm unsure how to read it and store its contents in an array-like form that I can pass to a function that then loops through the contents.
So, basically I want something like (pseudo code):
VariableArray var[] = new Variable (get the file contents);
performFunction(var[]);
How I can accomplish this?
You can use this to get an InputStream to your file in assets.
InputStream is = getAssets().open(fileName);
Then you just need to make a reader and loop through the file until the end of it.
I suggest you to use the JavaCSV library to deal with CSV formats. There are a lot of things it can accomplish in very few lines of code.

How can I create an external shared pref/config file for my Android application?

I am looking for a way to store configuration setting on an external file on the sd card. The app must look into this external file and then retrieve some kind of settings. At the moment i am trying to get it to look into a file and get a name. I know you can store things in shared preferences but they are internally accessed.
Anybody know an external way? Thanks
The idea is to have a simple text file or xml file on the sd card. So when a configuration needs changing it is done thru that file?
EDIT
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/Config.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c;
while ((c = br.read()) != -1) {
line+=(char)c;
if(String.valueOf(c) == ";" & line =="name;"){
String name ="";
}
}
}
I have attempted to read in a config.txt file and to separate each value with a ;
But i cant seem to understand exactly how i am going to attach what comes after name; to the variable String name.
the config.txt file has the following:
name;fred
somethingelse;test
The program should know when it has got to name and then set the name variable to fred??
Hi
I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values. When same development came to android the developers (I was not on android at that time.) This is what they have done.
We had created a text file like this
"name";"value";"data_type"
For example
"application_runs";"1";"int"
Or
"trial_period_key";"01ab23cd";"string"
The data type is hardcoded so we can detect the datatypes. For variable names we also had hardcoded list of preferences. And values can be parsed accordingly. They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.
The drawback of this procedure is
You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)
The text file on SD Card must be readable, you have to program it along with parsing.
Hope it helps.

external xml file integration in 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);

Categories

Resources