// Prepare the DOM document for writing
Source source = new DOMSource(node);
// Prepare the output stream
Result result = new StreamResult(stream);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
…抛出。。
javax.xml.transform.TransformerException: java.io.IOException: Detected invalid substitute UTF-16: da89 4f ?
(另外,我不知道为什么,但是这个异常是vm在我的语言葡萄牙语中提出的唯一一个异常,作为“Detectado substituto UTF-16 invlido”,我已经将它翻译为“Detected invalid UTF-16 substitute”)
另一件奇怪的事情是,我用的是UTF-8,而不是UTF-16,在我的文本中,我已经检查过了。而且,我相信如果UTF是问题所在,它不会导致随机的异常,因为我得到了相同数量的文本来转换为XML。
这种异常很难重现,因为它并不总是发生,而且在将大量数据转换为XML时也会发生。