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);
Related
I have seen many question about accessing files in assets folder but can't seem to get a solid answer. I'm working on a application that would extract a text from PDF file thus I'm using iText Library to do that but my problem here is the file pathing of PDF.
I have tried using assetManager and assetManager opens that file but i think iText needs the file path so it can open the file by itself, i am not sure but that is my theory.
PdfReader reader = new PdfReader(<String PDF-file>);
then How do I access the file under assets folder using iText? if it is not possible is there a way to do that?
Here is sample code to get asset directory for file and use it for pdfreader.
code:
File f = FileUtils.fileFromAsset(this, "filename.pdf");
PdfReader pdfReader = new PdfReader(f.getPath());
You can get an InputStream object like this
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
and then use this constructor
public PdfReader(InputStream is)
throws IOException
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#PdfReader(java.io.InputStream)
I have solved my problem my using this:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));
I am developing an app that needs to refer to data in a number of XML files. There are a lot ( ~40) XML files required by the app.
The file have names like "AbnormalFlags-v1.0.xml" and I current have them located in
res/raw/v2/AbnormalFlags-v1.0.xml
res/raw/v2/ActualDeliveryPlace-v1.0.xml
res/raw/v2/AddressType-v1.0.xml ... ...
A number of questions
Is this the best place to locate these files ?
The filenames seem to be a problem, will that cause issues
Because I have to dynamically determine which file to use I use the:
xmlFile = getApplicationContext().getResources().getIdentifier(xmlFileId,"raw","com.apps4health.refpack");
This does not find the resource file, where am I going wrong ?
Given the format of the file names I would suggest you to save them in the assets folder. Create a folder named assets inside the res folder and put there the XML files.
The code below shows how to open files in the assets directory:
InputStream is = null;
try {
AssetManager am = getApplicationContext().getAssets();
is = am.open("AbnormalFlags-v1.0.xml");
// Process the XML file from "is"...
}catch( IOException e ) {
// Handle exception
}
AssetManager is responsible to open files saved in the res/assets folder. You just call the open() method passing the file name as argument.
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.
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.
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);