2013-06-27 124 views
1

我有一個持有人的數據的對象。當用戶點擊下載按鈕時,xml需要由(person.dataitems)創建,之後用戶可以選擇下載文件(如保存文件或打開文件)如何在沒有中間文件存儲的情況下下載XML文檔?

我已經寫下了下面的代碼它在點擊按鈕時創建一個xml文件,但該文件保持空白。我想知道如何將數據寫入此文件然後下載。

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(); 
} 

謝謝。

+1

你從不在你的'StringWriter'中放入任何東西。 –

+0

@ user1882758可能是您應該重新標題您的問題:更類似於:'如何使XML文檔可下載沒有中間文件存儲'...只是一個建議;) –

回答

2

你必須寫到您的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的所有內容。

務必:

r.write("some content"); 

和訪問由作家將包含字符串,這樣做:

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

編輯:

好了,你問與您提供的鏈接中的內容相距甚遠,除了您必須寫入StringWriter而不是File

這是可以實現這種方式:

1)不要建立一個XML文檔:

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")); 

你告訴我,如果還有什麼不清楚的話。

+0

我想寫入一個XML文件,所以我如何使用StringWriter將元素添加到XML文件中。我經歷了這個例子:http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ 但是,這是直接將文件保存在位置。我想提供一個下載選項。 – user1882758

+0

@ user1882758我更新了我的答案。試試看,並告訴我它是如何工作的) –

+1

它的工作!謝謝! – user1882758

相關問題