xml转换编码问题

发布于 2024-11-29 04:12:29 字数 936 浏览 5 评论 0原文

您好,我有一个简单的代码:

    InputSource is = new InputSource(new StringReader(xml))
    Document d = documentBuilder.parse(is)
    StringWriter result = new StringWriter()
    DOMSource ds = new DOMSource(d)
    Transformer t = TransformerFactory.newInstance().newTransformer()
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    t.setOutputProperty(OutputKeys.STANDALONE, "yes");
    t.setOutputProperty(OutputKeys.ENCODING,"UTF-16")
    t.transform(ds,new StreamResult(result))
    return result.toString()

应该将 xml 转换为 UTF-16 编码。虽然据我所知,jvm 中 String 的内部表示已经使用 UTF-16 字符,但我的期望是结果 String 应该包含一个标头,其中编码设置为“UTF-16”,originla xml 为 UTF-8但我得到:(

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>

独立属性似乎也是错误的) 变压器实例是:com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl (我认为这是默认的) 那么我想念这里什么呢?

Hi I have a simple code:

    InputSource is = new InputSource(new StringReader(xml))
    Document d = documentBuilder.parse(is)
    StringWriter result = new StringWriter()
    DOMSource ds = new DOMSource(d)
    Transformer t = TransformerFactory.newInstance().newTransformer()
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    t.setOutputProperty(OutputKeys.STANDALONE, "yes");
    t.setOutputProperty(OutputKeys.ENCODING,"UTF-16")
    t.transform(ds,new StreamResult(result))
    return result.toString()

that should trasnform an xml to UTF-16 encoding. Although internal representation of String in jvm already uses UTF-16 chars as far I know, but my expectations are that the result String should contain a header where the encoding is set to "UTF-16", originla xml where it was UTF-8 but I get:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>

(also the standalone property seems to be wrong)
The transformer instance is: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl
(what I think is a default)
So what I miss here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-12-06 04:12:29

使用明确声明 UTF-16 作为输出编码的编写器。尝试使用 OutputStreamWriter(OutputStream out, String charsetName) 来包装 ByteArrayOutputStream 并查看是否有效。

Use a writer where you explicitly declare UTF-16 as output encoding. Try OutputStreamWriter(OutputStream out, String charsetName) which should wrap aByteArrayOutputStream and see if this works.

感情旳空白 2024-12-06 04:12:29

我现在自己写了一个测试。有一个小的变化:

 t.transform(ds,new StreamResult(new File("dest.xml")));

我有相同的结果但是该文件确实是 UTF-16 编码的,用十六进制编辑器检查。由于某些奇怪的原因,xml 声明没有更改。所以你的代码可以工作。

I have wrote a test on my own now. With one minor change:

 t.transform(ds,new StreamResult(new File("dest.xml")));

I have the same results but the file is indeed UTF-16 encoded, checked with a hex editor. For some strange reason the xml declaration is not changed. So your code works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文