开发者

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

   Employ开发者_如何学编程eeDetailHandler 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜