-1

How can I download an XML file from a URL and convert the XML file to a string? In particular, I want to download an RSS feed and convert it to a string.

I tried this, but it didn't work:

protected List<Rss> doInBackground(Void... params) {

       String xmlContent;
       Document doc  = null;

        try {
            doc = Jsoup.connect(feedUrl).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        xmlContent = doc.toString();
        return RssParser.parseFeed(xmlContent);
    }
pillravi
  • 4,035
  • 5
  • 19
  • 33
  • Possible duplicate of [How to download XML file from server and save it in SD card?](http://stackoverflow.com/questions/8986376/how-to-download-xml-file-from-server-and-save-it-in-sd-card) – Tristan Mar 09 '16 at 19:06
  • When you download it, it *is* a string. – Scott Hunter Mar 09 '16 at 19:07
  • Use retrofit to download and then use simple xml converter with retrofit http://square.github.io/retrofit/ – Roee Nov 27 '17 at 14:18

3 Answers3

2

You can use HttpURLConnection, Sample code:

String xmlString;
HttpURLConnection urlConnection = null;
URL url = new URL("http://example.com");
String userName = "test";
String password = "password";
try {
    urlConnection = (HttpURLConnection)url.openConnection();
    // set authentication
    String userCredentials = userName+":"+password;
    String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes(), Base64.DEFAULT));
    urlConnection.setRequestProperty("Authorization", basicAuth.trim());
    // set request method
    urlConnection.setRequestMethod("GET");
    if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        StringBuilder xmlResponse = new StringBuilder();
        BufferedReader input = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()), 8192);
        String strLine = null;
        while ((strLine = input.readLine()) != null) {
             xmlResponse.append(strLine);
        }
        xmlString = xmlResponse.toString();                
        input.close();
    }
  }
  catch (Exception e) {// do something

  }
  finally {// close connection
    if (urlConnection != null) {
        urlConnection.disconnect();
      }
 }
Puneet Arora
  • 199
  • 7
1

Use a URLConnection or the HttpUrlConnection subclass to download a file.

http://developer.android.com/reference/java/net/URLConnection.html

use a StringBuilder class to take the file contents and then toString() when done.

http://developer.android.com/reference/java/lang/StringBuilder.html

Oracle connection example:

https://docs.oracle.com/javase/tutorial/networki/urls/readingWriting.html

mjstam
  • 1,049
  • 1
  • 6
  • 5
1

Follow this on the Android Developer Website.

Basically you just need to use the following classes:

URL - Create new URl.

HttpURLConnection - Open connection to the URL

InputStream - get the response.

Make sure you are using AsyncTask or secondery Thread to approach the web, dont do it on the main thread

the response will come back as a String automaticaly, it dosent matter if you are opening connection to XML file, JSON file or even an IMAGE (not realy readable but still a string) file.

I will try to edit and write a full answer later if you still need it.

Roee
  • 1,155
  • 3
  • 15
  • 24