私はそれがxmlファイルを返し保存内容
http://api.worldbank.org/countries/GBR/indicators/NY.GDP.MKTP.KD.ZG?date=2004:2012
などのリンクからxmlファイルを取得したい、私は "という名前の私のフォルダにこのファイルを保存する方法がわかりません実際に私はこのリンクの結果をユーザに表示したくないのですが、そのようなリンクを動的に生成しています。
助けてください!
私はそれがxmlファイルを返し保存内容
http://api.worldbank.org/countries/GBR/indicators/NY.GDP.MKTP.KD.ZG?date=2004:2012
などのリンクからxmlファイルを取得したい、私は "という名前の私のフォルダにこのファイルを保存する方法がわかりません実際に私はこのリンクの結果をユーザに表示したくないのですが、そのようなリンクを動的に生成しています。
助けてください!
この状況では、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.
ありがとうございます!
ありがとうsarath、ありがとうございました。 – Sid
ところで、jsoupとファイルsarath.xmlが格納されている場所に、そのファイルが見つかりません。 – Sid
上記のコードで2つのステートメントを考えてみましょう:Document document = Jsoup.connect(url).get(); xmlContent.append(document.body()。html());ここでは、jsoupを使用してURLに接続し、接続されたURLからHTMLソースをダウンロードします。 xmlファイルはURLのHTML
の中にあるので、document.body()。html()を使用して本文からXMLコンテンツを抽出することができます。 –あなたの体はHTMLではなくXMLです。Apache HttpClientを使用して取得し、読み込まれたInputStreamをFileOutputStreamにポンピングするだけです。なにが問題だったの?解析されたコンテンツをフォーマットされた形式で保存しますか?
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;
}
システムの一時的なディレクトリに保存したい、私はそれをサーバーに保存したい、それはクライアント側に行く... – Sid
お客様のクライアントアプリケーションは何と書かれていますか? HTML5またはJava? – Perception
これはJava(struts2)で書かれていますが、JavaScriptを試してみましたが問題はユーザーの介入が必要なことです。 – Sid