Rectangle 27 0

java Parse XML on Android using inputstream from Cache?


public final static Document XMLfromString(String xml)
{
    if(xml!=null)
    {
    /* Replace the & in string with &amp (if any) */
        String xmlNew=clearRefs(xml);
        Document doc = null;
    //  Log.v(tag,"After Clearing refs::"+xmlNew);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Log.v(tag,"Trying Obtaining Document");
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlNew));
            doc = db.parse(is); 
        } catch (ParserConfigurationException e) {
            Log.e(tag,"XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            connectionStatus="No data received";
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            Log.e(tag,"I/O exeption: " + e.getMessage());
            connectionStatus="No data received";
            return null;
        }
        return doc;
    }
    else
    {
        connectionStatus="Null string Passed";
        return null;
    }
}

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.

This works irrespective of where the file was obtained from.

Note
Rectangle 27 0

java Parse XML on Android using inputstream from Cache?


SAXParserFactory fabrique = SAXParserFactory.newInstance();
    SAXParser parseur = null;
    ArrayList temp = null;
    try {
        parseur = fabrique.newSAXParser();
        DefaultHandler handler = new ParserXMLHandler();

        File fs=context.getFilesDir();
        File file = new File(fs.getAbsoluteFile(),"Myfile.xml");
        parseur.parse(file, handler);


    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
try {
            URL   urlxml = new URL(YOUR_XML_FILE_URL);
            URLConnection ucon = urlxml.openConnection();
            ucon.setConnectTimeout(4000);
            ucon.setReadTimeout(4000);
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, 128);
            FileOutputStream fOut =this.openFileOutput("myFile.xml", Context.MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            int length = ucon.getContentLength();
            int current = 0;
            long total = 0;
            while ((current = bis.read()) != -1) {
                total += current;
                osw.write((byte) current);
            }
            osw.flush();
            osw.close();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            finishUpdate = false;
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
            finishUpdate = false;
        }

And for retreving and parsing the downloaded xml file use :

Thanks for the answer. This would have worked if I didn't had the restriction of using same function for both files (One on internet and one stored locally). Also I cannot use SAXParser. I am using javax.xml.parsers. But your answer gave me an idea on a different problem that I had. Thanks

Whene you are downloading the xml file use this code:

Note
Rectangle 27 0

java Parse XML on Android using inputstream from Cache?


Nopes it does not work. It still gives the same error: "Is a directory". What baffles me in this problem is that the same function and code works if I try to read from /mnt/sdcard/Fmobile/Cache while not from the /data/data/com.example.app1/cache/ given by getFilesDir() or even from getCacheDir(). Now since this works with external storage I think it might have something to do with permission or how android handles internal storage.

The db.parse is taking "file" and returning "document".

Note