Take the 2-minute tour ×

I have a xml file in remote place.I want to use that xml file in my android project.can anyone give me a example code for this.I am using android 2.2.

P.S : I can access the local xml file which is in /res folder. I don't know anything about xPath.

share|improve this question
Something interesting... If you want to set it as layout in your activity then I think You can't.. –  user370305 Jan 13 '12 at 5:24
thats just a xml file contains some raw data.Here am not taking about layout/manifest xml. –  advishnuprasad Jan 13 '12 at 5:47
Then you can use it by valid xml parser.. –  user370305 Jan 13 '12 at 5:48
well i have used document builder and doc builder factory for accessing the content of the xml..My problem is how to retrieve the xml file which is in remote place.I have use HTTPUrlConnection but it is giving runtime error... –  advishnuprasad Jan 13 '12 at 5:54
you have to use WS –  Sameer Jan 13 '12 at 6:09
add comment

2 Answers

up vote 2 down vote accepted
try {
            //set the download URL, a url that points to a file on the internet
            //this is the file to be downloaded
            URL url = new URL("http://IP/Downloads/data.xml");

            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //and connect!

            //set the path where we want to save the file
            //in this case, going to save it on the root directory of the
            //sd card.
            File SDCardRoot = Environment.getExternalStorageDirectory();
            //create a new file, specifying the path, and the filename
            //which we want to save the file as.
            File file = new File(SDCardRoot,"data.xml");

            //this will be used to write the downloaded data into the file we created
            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();

            //variable to store total downloaded bytes
            int downloadedSize = 0;

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                    //add the data in the buffer to the file in the file output stream (the file on the sd card
                    fileOutput.write(buffer, 0, bufferLength);
                    //add up the size so we know how much is downloaded
                    downloadedSize += bufferLength;

            //close the output stream when done
            //catch some possible errors...
    } catch (MalformedURLException e) {
    } catch (IOException e) {

Try this code for downloading your xml file and check this tutorial for reading xml http://www.java-samples.com/showtutorial.php?tutorialid=152

share|improve this answer
instead of saving xml in my sd card am using that dynamically for my search query.ty fr the link –  advishnuprasad Jan 18 '12 at 5:05
Couldnt get u ..?? what do u mean by that dynamically for my search query. –  Mohit Sharma Jan 18 '12 at 6:09
add comment

if you are trying to set layout or drawable or style by remote xml , you can't.

share|improve this answer
No.i just want to use the xml data for some other purpose.not to set layout –  advishnuprasad Jan 13 '12 at 5:44
Ok, then use http connection to get that xml in response, and parse it. –  jeet Jan 13 '12 at 6:04
exactly...there only i have a problem. URL url = new URL("something.com/vishnu.xml"); urlConnection = (HttpsURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); –  advishnuprasad Jan 13 '12 at 6:06
i have used http as well –  advishnuprasad Jan 13 '12 at 6:08
If you are developing on Android 2.2 or later, you can take advantage of the javax.xml.xpath's functionality (introduced in API level 8). –  jeet Jan 13 '12 at 6:15
show 1 more comment

Your Answer


