Search code examples
javaandroidxmlinternal-storage

Downloading XML File and store it on internal storage


I want to download an XML-File from Webserver directly in my own directory.

My Code for creating a new Folder in Internal Storage is

File mydir = context.getDir("LENOX",Context.MODE_PRIVATE);

File fileWithinDir = new File(mydir,"LENOX.xml");

My Class for Downloading XML-Files:

public class XMLParser {
ConnectionDetector cd = new ConnectionDetector(ListViewActivity.getAppContext());




public String getXMLFromUrl(String url) {
    String xml = null;

    if (cd.isConnectingToInternet()) {
        try {
            //defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        return null;
    }

    return xml;
    //return xml
}

public Document getDomElement(String xml) {

    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
    } catch (ParserConfigurationException e) {
        Log.e("Error Parser: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error SAX: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error IO: ", e.getMessage());
        return null;
    }
    return doc;
}

public String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return this.getElementValue(n.item(0));
}

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

from here

Code Snippet... maybe it helps understanding

XMLParser parser = new XMLParser();
            String xml = parser.getXMLFromUrl("http://testsite.de/XXX.xml");

Is there any code snippet for creating a File in my own directory from downloaded xml file?


Solution

  • if you want to write String inside a file, you can use this method:

     public static void writeStringToFile(String content, File file)throws IOException {
            FileWriter writer = new FileWriter(file);
            writer.write(content);
            writer.flush();
            writer.close();
        }
    

    however, i recomand you to use android SharedPreferences. it is most practical.

    public static void writeStringToPreference(Context context,String tag,String    value)
      {
         SharedPreferences sharedP = context.getSharedPreferences(nameSpace, O);
         Editor editor=sharedP.edit();
         editor.putString(tag,value)
         editor.commit();
      }
    
    public static String readStringToPreference(Context context,String tag)  {
      SharedPreferences sharedP = context.getSharedPreferences(nameSpace, O);
      return sharedP.getString(tag);
     }