Java、XML DocumentBuilder - 解析时设置编码

Java, XML DocumentBuilder - setting the encoding when parsing(Java、XML DocumentBuilder - 解析时设置编码)
本文介绍了Java、XML DocumentBuilder - 解析时设置编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试将保存 XML 文档的树(扩展 JTree)保存为已更改其结构的 DOM 对象.

I'm trying to save a tree (extends JTree) which holds an XML document to a DOM Object having changed it's structure.

我创建了一个新的文档对象,遍历树成功检索了内容(包括XML文档的原始编码),现在有了一个ByteArrayInputStream具有正确编码的树内容(XML 文档).

I have created a new document object, traversed the tree to retrieve the contents successfully (including the original encoding of the XML document), and now have a ByteArrayInputStream which has the tree contents (XML document) with the correct encoding.

问题是当我解析 ByteArrayInputStream 时,编码会自动更改为 UTF-8(在 XML 文档中).

The problem is when I parse the ByteArrayInputStream the encoding is changed to UTF-8 (in the XML document) automatically.

有没有办法防止这种情况并使用 ByteArrayInputStream 中提供的正确编码.

Is there a way to prevent this and use the correct encoding as provided in the ByteArrayInputStream.

值得补充的是,我已经使用了
transformer.setOutputProperty(OutputKeys.ENCODING, encoding) 方法来检索正确的编码.

It's also worth adding that I have already used the
transformer.setOutputProperty(OutputKeys.ENCODING, encoding) method to retrieve the right encoding.

任何帮助将不胜感激.

推荐答案

这是一个更新的答案,因为 OutputFormat 已被弃用:

Here's an updated answer since OutputFormat is deprecated :

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("
|
", "");

第二部分将 XML 文档作为字符串返回

The second part will return the XML Document as String

这篇关于Java、XML DocumentBuilder - 解析时设置编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)