2012-01-13 95 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; 

}