代码之家  ›  专栏  ›  技术社区  ›  Student

随机变换异常,如何解决?

  •  1
  • Student  · 技术社区  · 16 年前

    // 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时也会发生。

    2 回复  |  直到 16 年前
        1
  •  0
  •   user207421    16 年前

    从输入流(而不是读取器)构造源代码,并让XML解析器找出字符集。

        2
  •  0
  •   Student    16 年前

    我认为解决办法是让程序在失败时重新执行操作。因为很难繁殖,所以不应该连续发生两次。