English 中文(简体)
如何保存被宣布为《javax.xml.transform》产出中的编码。 (例如,避免UTF-16改为UTF-8)
原标题:How to Preserve the Input s Declared Encoding in the Output of javax.xml.transform.Transformer.transform? (e.g. avoid UTF-16 changing to UTF-8)

假定这一投入XML

<?xml version="1.0" encoding="UTF-16"?>
<test></test>

撰写这些法典:

StreamSource source = new StreamSource(new StringReader(/* the above XML*/));
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory.newInstance().newTransformer().transform(source, streamResult);
return stringWriter.getBuffer().toString();

Outputs:

<?xml version="1.0" encoding="UTF-8"?>
<test></test>

(UTF-16的公开编码改为“UTF-8)

I know I can explicitly ask for UTF-16 output

transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");

www.un.org/Depts/DGACM/index_spanish.htm 但问题是,如何使产出编码自动与投入相同?

最佳回答

为此,你不得不使用比“<代码”更尖端的东西。 页: 1 例如,>>>>> getCharacterEncodingScheme(> 能够把产出随附起来的一套方法告诉你。

问题回答

尝试:

// Create an XML Stream Reader
XMLStreamReader xmlSR = XMLInputFactory.newInstance()
        .createXMLStreamReader(new StringReader(/* the above XML*/));
// Wrap the XML Stream Reader in a StAXSource
StAXSource source = new StAXSource(xmlSR);
// Create a String Writer
StringWriter stringWriter = new StringWriter();
// Create a Stream Result
StreamResult streamResult = new StreamResult(stringWriter);
// Create a transformer
Transformer transformer = TransformerFactory.newInstance().newTransformer();
// Set STANDALONE based on the source stream
transformer.setOutputProperty(OutputKeys.STANDALONE,
        xmlSR.isStandalone() ? "yes" : "no");
// Set ENCODING based on the source stream
transformer.setOutputProperty(OutputKeys.ENCODING,
        xmlSR.getCharacterEncodingScheme());
// Set VERSION based on the source stream
transformer.setOutputProperty(OutputKeys.VERSION, xmlSR.getVersion());
// Transform the source stream to the out stream
transformer.transform(source, streamResult);
// Print the results
return stringWriter.getBuffer().toString();

You need to peek into the stream first. Section F of the XML specification gives you an idea how to auto-detect the encoding.

XSLT处理器实际上知道输入编码是什么(XML教区长告诉它,因为不需要知道)。 您可以使用xsl:output确定产出编码,但这样做与投入一样,使你不得不首先发现输入编码,例如,在投放之前在源文档中沉积。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签