最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Save content of HTML in local storage - Stack Overflow

programmeradmin3浏览0评论

i want to fetch xml file from the links like

.GDP.MKTP.KD.ZG?date=2004:2012

it returns a xml file, i don't know how to save this file in my folder named "temp" using java or javascripts, actually i don't want to display this result of that link to the user, I'm generating such links dynamically.

please help!!!

i want to fetch xml file from the links like

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

it returns a xml file, i don't know how to save this file in my folder named "temp" using java or javascripts, actually i don't want to display this result of that link to the user, I'm generating such links dynamically.

please help!!!

Share Improve this question edited Mar 4, 2012 at 14:31 PinoSan 1,50816 silver badges27 bronze badges asked Mar 3, 2012 at 18:46 SidSid 1282 silver badges10 bronze badges 2
  • What is your client application written in? HTML5 or Java? – Perception Commented Mar 3, 2012 at 19:39
  • it has been written in Java(struts2), i tried JavaScript but problem is that it needs a user intervention, i dnt need user in between – Sid Commented Mar 3, 2012 at 22:30
Add a ment  | 

3 Answers 3

Reset to default 10

I remend you to use an HTML parser library like jsoup in this situation. Please have a look at the below steps for better under standing:

1. Download jsoup core library  (jsoup-1.6.1.jar) from http://jsoup/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 .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 pleted successfully..!");
    }

    public static void downloadXml() throws IOException {
        String url = "http://api.worldbank/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.

Thank you!

Well your body is XML not HTML, just retrieve it using Apache HttpClient, and pump the read InputStream to a FileOutputStream. What was the problem? Do you want to save parsed content in a formatted form?

public String execute() {
        try {
            String url = "http://api.worldbank/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)

  1. 暂无评论