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

如何在java中将大字符串保存到xml文件中?,java,xml,Java,Xml,我将一些xml格式的大数据加载到程序中的一个字符串中(来自mysql),当我将这个字符串保存到out.xml中时,只存储了大约500条记录。 如何将大字符串或其他数据以xml格式或任何其他格式保存到文件中 这是我的密码: String xmlString = doExportIntoString(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); InputSource sou

我将一些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) {}
    }
}

上面的代码不正确。如果在write中抛出异常,那么writer将不会关闭。我认为这与此无关。我还可以指出,你们试着在边上接球,你们的接球也有一个终点。如果你捕获到一个异常,会发生什么?坚持这个问题,错误捕获不是这里的问题。这是java的习惯用法。然而,如果在试图关闭writer时发生异常,我们将无能为力。
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) {}
    }
}