I want to parse a XML file from a server on internet and save it locally for some time so that if the connectivity is lost I can still see the information on app.
I am able to successfully parse the XML file from the internet. I am also able to save it in the cache directorygetCacheDir(), files directorygetFilesDir() and external storage.
Now, I want to parse the local XML file and still want to use the same function that I used to parse the XML from internet. However after several tries and hours of searching I am not able to parse the inputstream from the file stored in Cache directory or the Files directory. The only directory the function works on is the external directory.
So this code WORKS:
inputstream is;
File file = new File ("/mnt/sdcard/Fmobile/Cache","Newfile.xml");
is = new BufferedInputStream(new FileInputStream(file));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document dom = db.parse(is);
While the following code does NOT work.
inputstream is;
File file = new File (getFilesDir(),"Newfile.xml");
is = new BufferedInputStream(new FileInputStream(file));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document dom = db.parse(is); **// Error is given on this line : "Is a directory"**
Can anyone tell me what is the difference between the above two code or what should be done to get the inputstream from the Cache directory or the Files directory.
Just so you know I have tried replacing the getFilesDir() with the path to the file.
Most solutions I have searched for talk about storing the file in asset or res folder but I cannot do that because it is a dynamic file which will keep changing and that’s why I need to use cache and timeout.
I am sorry for any mistakes that I might have made while posting this. This is my first post.
The Log Cat is as follows:
04-04 11:27:24.579: I/com.example.app1.FMobileActivity(16029): Running Form [2] Local File
04-04 11:27:24.579: I/com.example.app1.FMobileActivity(16029): ProcessForm Data2
04-04 11:27:24.579: I/com.example.app1.FMobileActivity(16029): /data/data/com.example.app1/cache/Newfile2.xml
04-04 11:27:24.589: E/com.example.app1.FMobileActivity(16029): Error occurred in ProcessForm:Is a directory
04-04 11:27:24.589: W/System.err(16029): java.io.IOException: Is a directory
04-04 11:27:24.599: W/System.err(16029): at org.apache.harmony.luni.platform.OSFileSystem.read(Native Method)
04-04 11:27:24.599: W/System.err(16029): at dalvik.system.BlockGuard$WrappedFileSystem.read(BlockGuard.java:165)
04-04 11:27:24.599: W/System.err(16029): at java.io.FileInputStream.read(FileInputStream.java:290)
04-04 11:27:24.599: W/System.err(16029): at java.io.FileInputStream.read(FileInputStream.java:245)
04-04 11:27:24.609: W/System.err(16029): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:140)
04-04 11:27:24.609: W/System.err(16029): at java.io.BufferedInputStream.read(BufferedInputStream.java:225)
04-04 11:27:24.609: W/System.err(16029): at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1044)
04-04 11:27:24.609: W/System.err(16029): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:114)
04-04 11:27:24.609: W/System.err(16029): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)
04-04 11:27:24.609: W/System.err(16029): at com.example.app1.FMobileActivity.GetFormData(FMobileActivity.java:315)
04-04 11:27:24.609: W/System.err(16029): at com.example.app1.FMobileActivity.onCreate(FMobileActivity.java:58)
04-04 11:27:24.609: W/System.err(16029): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 11:27:24.609: W/System.err(16029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
04-04 11:27:24.609: W/System.err(16029): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1727)
04-04 11:27:24.609: W/System.err(16029): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
04-04 11:27:24.609: W/System.err(16029): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:974)
04-04 11:27:24.609: W/System.err(16029): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 11:27:24.609: W/System.err(16029): at android.os.Looper.loop(Looper.java:130)
04-04 11:27:24.609: W/System.err(16029): at android.app.ActivityThread.main(ActivityThread.java:3859)
04-04 11:27:24.609: W/System.err(16029): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 11:27:24.609: W/System.err(16029): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 11:27:24.609: W/System.err(16029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
04-04 11:27:24.609: W/System.err(16029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
04-04 11:27:24.609: W/System.err(16029): at dalvik.system.NativeStart.main(Native Method)
04-04 11:27:24.609: E/com.example.app1.FMobileActivity(16029): Couldn't parse the Form.
04-04 11:27:24.689: D/dalvikvm(16029): GC_EXTERNAL_ALLOC freed 118K, 48% free 2837K/5379K, external 2743K/2773K, paused 33ms
I ended up using the following to get a document from a string. String can be obtained by downloading a file or by reading a file locally. SO it solves the problem I had. If anyone needs help in getting the contents of local file as a string let me know in comments. It is fairly simple same fro fetching data from online server.