2

I want to save the data from a certain link in a xml file in android. How can I do this? Can anyone help me, I have no idea.

Thanks in advance.

The data from link looks like this :

 <elements>
   <element>
     <def>confirm</def>
     <text>Ok</text>
   </element>
  <element>
    <def>email</def>
    <text>Email</text>
  </element>
  <element>
   <def>firstname</def>
   <text>Prénom</text>
  </element>
</element>
Gabrielle
  • 4,933
  • 13
  • 62
  • 122
  • can you elaborate your requirement a bit more? What link? What kind of content it will be? How to map the content to an XML file? Where to store it? – dongshengcn Aug 22 '11 at 14:01
  • I want to save that data in a xml file in app, in the same format like it is at link. The content is like I put before, in this format – Gabrielle Aug 22 '11 at 14:08

1 Answers1

3

You are not asking a specific question. You can not expect other people to write code for you. Sorry, do not try to be mean. Anyways, to get you start, I assume you are trying to download a file from a http link? You might want to look at httpClient. This is a good example of using it:

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class TestHttpGet {
    public void executeHttpGet() throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI("http://www.somewhere.com/somefile.xml"));
            HttpResponse response = client.execute(request);
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String page = sb.toString();
            System.out.println(page);
            } finally {
            if (in != null) {
                try {
                    in.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
dongshengcn
  • 6,434
  • 7
  • 35
  • 44