![](/img/trans.png)
[英]Special characteres error on saving XML file in Java (“<” and “>”)
[英]Java error saving xml
我在Java中有一个Document对象,我想保存它,所以我有以下代码:
TransformerFactory fabTransformador = TransformerFactory.newInstance();
Transformer transformador = fabTransformador.newTransformer();
Source origin = new DOMSource(documentoXML);
Result destino = new StreamResult(new java.io.File(nombrearchivo));
transformador.transform(origin, destino);
其中“ nombrearchivo”是文件名(file.xml), documentoXML
是Document对象。 执行代码时,我收到以下输出:错误:”我没有收到任何异常,仅收到错误消息:”该文档大约有1.3 GB,我不知道这是问题所在,在这种情况下,还有另一种保存文件的方法吗?
我使用下一个导入:
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
您的代码看起来不错,但是作为参考,您可以使用另一种方法来保存DOM文档:
import org.w3c.dom.ls.*;
import java.io.FileOutputStream;
DOMImplementationLS ls = (DOMImplementationLS)documentoXML.getImplementation();
LSSerializer ser = ls.createLSSerializer();
LSOutput out = ls.createLSOutput();
out.setEncoding("UTF-8");
FileOutputStream output = new FileOutputStream("output.xml");
try {
out.setByteStream(output);
ser.write(documentoXML, out);
} finally {
output.close();
}
使用以下命令确定确切原因:
try {
secure.generaTextoXML();
} catch (Trowable thr) {
thr.printStackTrace();
}
...或者尝试Level.ERROR
代替Level.SEVERE
为您记录:
try {
secure.generaTextoXML();
} catch (TransformerConfigurationException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.ERROR, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.ERROR, null, ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.