Search code examples
androidfileinputstream

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")));
}

Solution

  • 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);