Specify folder path to XML file in Android - 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.

Related

XmlPullParser setInput issue

I have problem with setInput() method. When I've provided plain StringReader and parse XML
locationsXml.setInput(new StringReader("<locations>" + "<location>" etc.[...]
Everything gone well, but if I want to load file from resources:
locationsXml.setInput(new FileReader("res/xml/locations.xml"));
File is not being loaded.
Where is the problem?
You have create a Inputstream from the file in your resources (provided the resource xml is placed in 'assets' folder). You can get control of that resource using :
context.getAssets.open(name of the file);
eg - InputStream myInput = myContext.getAssets().open(locations.xml);
Noww - you can use this inputStream to do anything you want.
Or
Put the xml into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you instance of XML parser.
InputStream object = this.getResources()
.openRawResource(R.raw.fileName);

can't open xml file on 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.

How to read an xml file from R.java

I have an xml file inside the res folder under the xml forlder. I want to parse it with SAX. I know there are lots of example but I want to understand InputStream, InputSource, File deeply. I can reach the file writing R.xml.hippo (hippo is the name of the xml file).
But how can I give this resource as a stream?
I know when I write R.xml.hippo to anywhere it will be reference(int) but I want give it as a source. What is and how converting an int to starting point of stream?
You can read XML from assets folder as well as from res/raw folder.
To read from assets folder
AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");
To read from raw folder
InputStream inputStream = getResources().openRawResource(R.raw.your_xml);
And parse this inputStream to your XML Parser.
You should place the file inside the raw folder. Then you can access it via Resources.openRawResource(). For more information refer to Android Developer's page.

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.

How to read local xml file is resource folder as a input stream in android?

I am trying to get input stream from something like this.
InputSource myInputSource = new InputSource(activity.getResources().openRawResource(com.MYCLass.R.xml.programs));
myXMLReader.parse(myInputSource);
and then call parse on the parser instance i Created. Some how i get nothing.
Works fine if I use a server XML....
Put the xml file into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you an instance of XML parser.
A piece of code taken from #Krupa
InputStream object = this.getResources()
.openRawResource(R.raw.fileName);
You can read the file by the following code :
InputStream object = this.getResources()
.openRawResource(R.rawFolderName.fileName);

Categories

Resources