Ad

Thursday, July 31, 2014

Download and parse XML in android

HttpClient client=new DefaultHttpClient();
            HttpGet request=new HttpGet(url);
            File xmlfile=new File(Environment.getExternalStorageDirectory()+"/Android/data/com.zacharia.app/", "fxml.xml");
            if(xmlfile.exists()){
                xmlfile.delete();
            }
            try{
                xmlfile=new File(Environment.getExternalStorageDirectory()+"/Android/data/com.zacharia.app/", "fxml.xml");
            HttpResponse response=client.execute(request);
            InputStream content = response.getEntity().getContent();           
            FileOutputStream output = new FileOutputStream(xmlfile);
           
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int len = 0;
            while ((len = content.read(buffer)) != -1) {
                output.write(buffer, 0, len);
            }
            content.close();
            output.close();


       
        try {
           
            InputStream is=    new FileInputStream(xmlfile);
            FamilyHandler handler=new FamilyHandler(context);
            SAXParser parser=SAXParserFactory.newInstance().newSAXParser();
            parser.parse(is, handler);
            return "success";
        } catch (SAXException e) {
            e.printStackTrace();
            // TODO: handle exception
            return "Unknown xml";
        }
        catch (Exception e) {
            e.printStackTrace();
            Log.i("zacharia", "Inside generate xml class exe : "+e);
            return "failed";
        }

No comments:

Post a Comment