I am trying to read data from an xml which is stored at sdcard.
So, I used XmlResourceParser in my code but how can I provide my xml file to XmlResourceParser?
Is there any other way to do the same?
File myXmlFile = new File(Environment.getExternalStorageDirectory()+"/myfiles/myxml.xml");
And try to read data from File...
Related
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")
Can I add .txt file in parse.com and retrieve it to my textview in my app. I know how to add a string and retrieve it but have a confusion regarding the .Txt file
When your creating your ParseFile object, you can give it a file type by adding the file extension to the end of the key name.
Eg:
ParseFile file = new ParseFile("sometext.txt", fileData);
will create a text file the the contents of fileData. This can be attached to a ParseObject and saved/retrieved from Parse the usual way.
By the way, you should give this question a parse.com tag.
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()
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 :)
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.