2012-01-13 135 views
1

我在遠程的地方有一個xml文件。我想在我的android項目中使用該xml文件。任何人都可以給我一個這樣的示例代碼。我使用的是android 2.2。在android中讀取遠程xml文件

P.S:我可以訪問位於/ res文件夾中的本地xml文件。 我對xPath一無所知。

+0

一些有趣的...如果你想在你的活動將其設置爲佈局那麼我認爲你不能.. – user370305 2012-01-13 05:24:42

+0

那只是一個XML文件包含了一些原始data.Here我不是採取關於佈局/清單xml。 – advishnuprasad 2012-01-13 05:47:34

+0

然後,您可以通過有效的XML解析器使用它.. – user370305 2012-01-13 05:48:27

回答

2
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! 
      urlConnection.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(); 
      progressDialog.setMax(totalSize); 

      //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 
      fileOutput.close(); 
      //catch some possible errors... 
    } catch (MalformedURLException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 

嘗試下載XML文件的代碼,並檢查本教程讀取XML http://www.java-samples.com/showtutorial.php?tutorialid=152

+0

,而不是保存xml在我的SD卡,我動態使用我的搜索query.ty fr鏈接 – advishnuprasad 2012-01-18 05:05:30

+0

Couldnt得到你.. ??我的搜索查詢動態地表達了什麼**。** – 2012-01-18 06:09:57

0

如果您嘗試通過遠程xml設置佈局或繪圖或樣式,則不能。

+0

No.i只是想使用XML數據的一些其他purpose.not設置佈局 – advishnuprasad 2012-01-13 05:44:54

+0

好吧,然後使用http連接來獲得該XML響應,並解析它。 – jeet 2012-01-13 06:04:47

+1

......只有我有一個問題。 URL url = new URL(「http://www.something.com/vishnu.xml」); urlConnection =(HttpsURLConnection)url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); – advishnuprasad 2012-01-13 06:06:25

0

更好的方法字符串讀取它,做任何你想用它做。

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

try 
{ 
    //default http client 
    HttpClient httpClient = new DefaultHttpClient(); 

    HttpPost httpPost = new HttpPost(url); 

    System.out.println("URL IN PARSER:==="+url+"===="); 

    HttpResponse httpResponse = httpClient.execute(httpPost); 

    HttpEntity httpentity = httpResponse.getEntity(); 

    xml = EntityUtils.toString(httpentity); // I have changed it... because  occur while downloading.. 

    Log.d("response", xml); 
} 
catch(UnsupportedEncodingException e) 
{ 
    e.printStackTrace(); 
} 
catch (ClientProtocolException e) 
{ 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    e.printStackTrace(); 
} 


return xml; 

}