簡體   English   中英

如何在java中將大字符串保存到xml文件中?

[英]How to save a large string into xml files in java?

我將一些大數據作為xml格式加載到我的程序中的一個字符串中(來自mysql),當我將這個字符串保存到out.xml中時,只存儲了大約500條記錄。 如何將大字符串或其他數據以xml格式或任何其他格式保存到文件中?

這是我的代碼:

    String xmlString = doExportIntoString();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    InputSource source = new InputSource(new StringReader(xmlString));
    Document document =factory.newDocumentBuilder().parse(source);        
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    Result result = new StreamResult(new File("C:\\xmlFile.xml"));
    Source s = new DOMSource(document);
    transformer.transform(s, result);

以下是上述代碼的正確版本:

BufferedWriter out;
try {
    out = new BufferedWriter(new FileWriter("out.txt"));
    out.write("aString");
} catch (IOException e) {
    throw new RuntimeException(e);    
} finally {
    if (out != null) {
        try { out.close(); } catch (IOException e) {}
    }
}
try {
    BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
    out.write("aString");
    out.close();
} catch (IOException e) {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM