can't open xml file on android - android

I am working on an android application in which I want to parse an xml file as a local resource. I use jdom to parse it, but I have a probleme, I can not open the file and I don't know why. The error is at this line :
document = builder.build(new File("res/xml/data.xml"));
The file is located into the folder res/xml of my project. I got this error:
java.io.IOException: Couldn't open file:/res/xml/data.xml
Caused by java.io.FileNotFoundException :res/xml/data.xml
I tried this:
document = builder.build(new File("data.xml"));
but it did not work. I don't know why the file is not found.
Would you have an idea?
Thanks in advance
Thank you for your suggestion. The 'Uri path' works but not the line
document = builder.build(new File(path));
The method class File must have a type entree String. I tried this:
document = builder.build(new File(path.getPath());
but it returned null and
document = builder.build(new File(path.toString());
I got the same error. Would you have an idea ?
Thanks in advance !!

I believe you will need to use an URI in your file constructor that points to the data.xml file.
Try something like this:
Uri path = Uri.parse("android.resource://my.package.here/" + R.xml.data);
document = builder.build(new File(path));
This tutorial here might also be helpful: http://androidbook.blogspot.com/2009/08/referring-to-android-resources-using.html

If this is a resource inside your application then you should use Resources methods suchas getXml(). You can get an instance of the rources class from the Context object

InputStream is = context.getResources().openRawResource(R.raw.data);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
document = builder.build(br);
Be sure to put the data.xml in the res/raw-folder.

Related

Kotlin: Cannot get FileReader to find file

For an Android App, I'm trying to use the FilerReader function to read my csv file, but after trying everything I cannot get to find my file. I'm using Android studios on a Mac.
Below is my code inside OnCreate():
val filereader = BufferedReader(FileReader("sgsraagadata.csv"))
I have tried placing the csv file inside assets & all resource folders including a raw-type folder.
Similar posts on Stackoverflow suggests that I find the directory that is being searched so that I can put the csv file in that directory. But when I follow their instructions below is what I get.
When I use:
println(System.getProperty("user.dir"))
It prints "/". And when I use:
println(File(".").getAbsoluteFile())
It prints "/."
Given these printed outputs, where should I put the csv file?
*Also added an image showing csv file in assets folder as per suggestion in comments. Result is still an file not found error.
Put your file in assets folder and open it like this:
val file = context.assets.open("sgsraagadata.csv")
val isr = InputStreamReader(file)
val reader = BufferedReader(isr)
var line = reader.readLine()
while (line != null) {
// do something with the line
line = reader.readLine()
}
supply a valid Context or inside an activity you can omit it.

access the file system

I put my files in the directory assets. how do I get them?
I tried following the instructions but have not worked:
File f = new File("/data/data/ant.BrowserX/files/" + pagCorrente + ".html");
File f = new File("file:///android_asset/" + pagCorrente + ".html");
there are no errors but running I can not access the file. How do I access the directory "assets"?
EDIT
thanks for the trick. I have the problem that I use a BufferedReader to read the file, and initialize it so, and not with an InputStream.
in = new BufferedReader(new FileReader(f));
how could I do?
You can get an InputStream from the file through the getAssets() method inside getResources():
InputStream is = getResources().getAssets().open("file.html");
Then just convert the stream to whatever you need
I know you can use this to get a inputstream :
final AssetManager assetMgr = this.getResources().getAssets();
InputStream is = assetMgr.open(pagCorrente +".html");
So probably you can use:
File f = new File(this.getResources().getAssets() + pagCorrente + ".html");
Simple answer, you don't access the "directory" assets. There is no directory structure in an .apk, it's just data. The framework knows how to access it (getAssets()), but you can't use it like a standard directory.
That being said, if you are trying to use a WebView and access a html file (which is what it looks like) they put a "fake" path reference in that you can use like this:
WebView wv = (WebView)this.findViewById(R.id.yourWebView);
wv.loadUrl("file:///android_asset/" + pagCorrente + ".html");

Specify folder path to XML file in Android

I put my employeedetailxml into a folder named "res\raw". When I attempt to open it by specifying the file and folder name, I get the error "file not found".
How can I specify the path to my file?
I would much prefer to be able to pass 'R.raw.employeedetailxml' to FileInputStream to specify that it open that file. Is that possible? (I tried it, and got an error.)
Can FileInputStream take a Resource ID as parameter?
try{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
EmployeeDetailHandler empDetailHandler=new EmployeeDetailHandler();
xr.setContentHandler(empDetailHandler);
xr.parse(new InputSource(new FileInputStream("\\res\\raw\\employeedetailxml.xml")));
}
Yes, you can use something similar to passing R.raw.employeedetailxml. You just need to fetch the resource XML file using that resource name, like so:
InputStream ins = getResources().openRawResource(R.raw.file_name);
Try this:
InputStream inputStream =
getResources().openRawResource(R.raw.employeedetailxml);
this.getResources().openRawResource() returns an InputStream that will probably help you to parse the xml.
If placing your xml file under assets doesn't hurt, you can try out this one:
AssetManager manager = getAssets();
InputStream inputStream = manager.open("pathUnderAssets/filename.xml");
Please note that getAssets() call is made on context. So, you must call it with context in classes which is not an Activity.

How to parse a local xml file using dom in android?

I have seen lots of examples about parsing local xml files with Sax but i need to do this with DOM. My xml file is under res/xml folder. Here is the code:
InputStream in = getResources().openRawResource(R.xml.maps);
StringBuffer str = new StringBuffer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Document document = db.parse(in, null);
document.getDocumentElement ().normalize ();
But it doesnt work. I think something missing with the line InputStream. It is really important for me, i need your help.
I had the same. I was wondering why this openRawResource gets the XML file corrupted, and that's because it was placed in r.xml.* not in r.raw.* :)
I have solved the problem. Just need to change small thing:
InputStream in = this.getResources().openRawResource(R.raw.bu);
I added "this" to that line and also I have put the maps.xml file under res/raw.

Android Reading Text File Line-by-Line From Res/Raw In Emulator, InvocationException On OpenRawResource

I've found through research on google that that I can read a text file by storing it in my res/raw folder and then accessing it through getResources().openRawResource(R.raw.words);
In a class WordHelper I the constructor provides throws an InvocationException on this line of code:
istream = getResources().openRawResource(R.raw.words);
Which is the one most examples seem to use without a problem, I will then go on to do this
isreader = new InputStreamReader(istream);
myReader = new BufferedReader(isreader);
once everything is working and then use the readLine() method.
All descriptions of InvocationException such as getCause are null, I definately have the file in the res/raw/words.txt.
Thanks for reading.
I got the same problem and solved it using isreader = new InputStreamReader(istream, "UTF8");

Categories

Resources