如何在不存储中间文件的情况下使XML文档可下载? [英] How to make an XML document downloadable without intermediate file storage?

查看:82
本文介绍了如何在不存储中间文件的情况下使XML文档可下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存一个人的数据的对象。当用户单击下载按钮时,需要由(person.dataitems)创建xml,然后该用户应该可以选择下载文件(例如保存文件或打开文件)

I have an object which holds data of a person. When a user clicks on the download button the xml needs to be created by (person.dataitems) and after that user should have an option to download the file (like save file or open file)

我编写了下面的代码,当单击按钮时,该代码创建了一个xml文件,但是该文件仍然为空。我想知道如何将数据写入此文件然后下载。

I have written the code below which creates a xml file when the button is clicked however the file remains empty. I would like to know how I can write data to this file and then download.

response.setHeader( "Content-Disposition", "attachment;filename="+patient.getGivenName()+".xml");   
try {
    StringWriter r = new StringWriter();
    String ccdDoc = r.toString();
    ccdDoc = ccdDoc.replaceAll("&lt;", "<");
    ccdDoc = ccdDoc.replaceAll("&quot;", "\"");
    byte[] res = ccdDoc.getBytes(Charset.forName("UTF-8"));
    response.setCharacterEncoding("UTF-8");
    response.getOutputStream().write(res);
    response.flushBuffer();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

谢谢。

推荐答案

您必须写入您的 StringWriter

import java.io.*;

public class StringWriterDemo {

   public static void main(String[] args) {

      String s = "Hello World";

      // create a new writer
      StringWriter sw = new StringWriter();

      // write portions of strings
      sw.write(s, 0, 4);
      sw.write(s, 5, 6);
      // write full string
      sw.write(s);

      // print result by converting to string
      System.out.println("" + sw.toString());


   }
}

不做:

String ccdDoc = r.toString();

仅创建 r 的副本串。然后,您将修改副本,但完全不修改 StringWriter 的内容。

It only creates a copy of the r string. Then you are modifying the copy, but not at all the content of the StringWriter.

执行:

r.write("some content");

并访问编写者包含的字符串,请执行以下操作:

and to access the string contained by the writer, do:

String a_string = r.toString();
response.getOutputStream().write(a_string);






编辑:

好,所以您要问的内容与您提供的链接中的内容相距不远,除了必须写入 StringWriter 而不是 File

OK, so what you are asking is not so far from what you have in the link you provided, excepted that you have to write into a StringWriter instead of into a File.

这可以通过以下方式实现:

This can be achieved this way:

1)生成xml文档:

1) Do build an xml document:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);

// staff elements
Element staff = doc.createElement("Staff");
rootElement.appendChild(staff);

// set attribute to staff element
staff.setAttribute("id", "1");

// firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("yong"));
staff.appendChild(firstname);

:
:

// Then write the doc into a StringWriter

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

//initialize StreamResult with StringWriter object to save to string
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);

String xmlString = result.getWriter().toString();
System.out.println(xmlString);

// Finally, send the response

byte[] res = xmlString.getBytes(Charset.forName("UTF-8"));
response.setCharacterEncoding("UTF-8");
response.getOutputStream().write(res);
response.flushBuffer();


要点

StreamResult result = new StreamResult(new StringWriter());

而不是:

StreamResult result = new StreamResult(new File("C:\\file.xml"));

您告诉我是否还有一些不清楚的地方。

You tell me if there is still something unclear in this.

这篇关于如何在不存储中间文件的情况下使XML文档可下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆