2012-03-03 157 views
3

我想從鏈接獲取XML文件像保存內容

http://api.worldbank.org/countries/GBR/indicators/NY.GDP.MKTP.KD.ZG?date=2004:2012

它返回一個XML文件,我不知道如何將這個文件保存我的文件夾名爲「臨時「使用java或javascripts,實際上我不想顯示這個鏈接的結果給用戶,我動態地生成這樣的鏈接。

請幫忙!!!

+0

你的客戶端應用程序是用什麼編寫的? HTML5還是Java? – Perception 2012-03-03 19:39:12

+0

它已被寫入Java(struts2),我試過JavaScript,但問題是,它需要用戶干預,我需要用戶之間 – Sid 2012-03-03 22:30:53

回答

10

我建議你在這種情況下使用像jsoup這樣的HTML解析器庫。請看下面的步驟以獲得更好的信譽:

1. Download jsoup core library (jsoup-1.6.1.jar) from http://jsoup.org/download 
2. Add the jsoup-1.6.1.jar file to your classpath. 
3. Try the below code to save the xml file from the URL. 

package com.overflow.stack; 

import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

/** 
* 
* @author sarath_sivan 
*/ 
public class XmlExtractor { 

    public static StringBuilder fetchXmlContent(String url) throws IOException { 
     StringBuilder xmlContent = new StringBuilder(); 
     Document document = Jsoup.connect(url).get(); 
     xmlContent.append(document.body().html()); 
     return xmlContent; 
    } 

    public static void saveXmlFile(StringBuilder xmlContent, String saveLocation) throws IOException { 
     FileWriter fileWriter = new FileWriter(saveLocation); 
     BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 
     bufferedWriter.write(xmlContent.toString()); 
     bufferedWriter.close(); 
     System.out.println("Downloading completed successfully..!"); 
    } 

    public static void downloadXml() throws IOException { 
     String url = "http://api.worldbank.org/countries/GBR/indicators/NY.GDP.MKTP.KD.ZG?date=2004:2012"; 
     String saveLocation = System.getProperty("java.io.tmpdir")+"sarath.xml"; 
     XmlExtractor.saveXmlFile(XmlExtractor.fetchXmlContent(url), saveLocation); 
    } 

    public static void main(String[] args) throws IOException { 
     XmlExtractor.downloadXml(); 
    } 

} 

4. Once the above code is executed successfully, a file named "sarath.xml" should be there in your temp folder. 

謝謝!

+0

謝謝sarath,對於你詳細的解決方案 – Sid 2012-03-04 17:12:45

+0

順便說一下,jsoup有什麼用和存儲sarath.xml文件的位置,我找不到該文件。 – Sid 2012-03-04 17:29:55

+0

考慮上述代碼中的兩個語句:Document document = Jsoup.connect(url).get(); xmlContent.append(document.body()。html());在這裏,我們使用jsoup連接到URL並從連接的URL下載HTML源代碼。由於xml文件位於URL的HTML 中,我們可以使用document.body()。html()從正文中提取XML內容。 – 2012-03-04 17:47:29

0

那麼你的身體是XML而不是HTML,只需使用Apache HttpClient檢索它,並將讀取的InputStream泵入FileOutputStream。有什麼問題?您是否想以格式化的形式保存解析的內容?

0
public String execute() { 
     try { 
      String url = "http://api.worldbank.org/countries/GBR/indicators/NY.GDP.MKTP.KD.ZG?date=2004:2012"; 
      String saveLocation = System.getProperty("java.io.tmpdir")+"sarath.xml"; 
      XmlExtractor.saveXmlFile(XmlExtractor.fetchXmlContent(url), saveLocation); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      addActionError(e.getMessage()); 
     } 
     return SUCCESS; 
    } 
+0

我想要它保存在系統的臨時目錄中,我希望它保存在服務器中,它將在客戶端... – Sid 2012-03-14 08:22:50