简体   繁体   English

DOMImplementationLS在Java中以UTF-8序列化为String

[英]DOMImplementationLS serialize to String in UTF-8 in Java

reading the documentation for java org.w3c.dom.ls it seems as a Element only can be serialized to a String with the java native string encoding, UTF-16. 阅读java org.w3c.dom.ls的文档似乎只能将元素序列化为带有java本机字符串编码UTF-16的String。 I need however to create a UTF-8 string, escaped or what not, I understand that it still will be a UTF-16 String. 但是,我需要创建一个UTF-8字符串,转义或不存在,我知道它仍然是一个UTF-16字符串。 Anyone has an idea to get around this? 任何人都有想法绕过这个? I need the string to pass in to a generated WS client that will consume the String, then it should be UTF-8. 我需要将字符串传递给将使用String的生成的WS客户端,然后它应该是UTF-8。

the code i use to create the string: 我用来创建字符串的代码:

DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.
DOMImplementationLS domImplementationLS = (DOMImplementationLS) REGISTRY.getDOMImplementation("LS");
LSSerializer writer = domImplementationLS.createLSSerializer();
String result = writer.writeToString(element);

You can still use DOMImplementationLS : 您仍然可以使用DOMImplementationLS

DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.
DOMImplementationLS domImplementationLS = (DOMImplementationLS)REGISTRY.getDOMImplementation("LS");
LSOutput lsOutput =  domImplementationLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
Writer stringWriter = new StringWriter();
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(doc, lsOutput);     
String result = stringWriter.toString();

I find that the most flexible way of serializing a DOM to String is to use the javax.xml.transform API: 我发现将DOM序列化为String的最灵活方法是使用javax.xml.transform API:

    Node node = ...
    StringWriter output = new StringWriter();

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(output));

    String xml = output.toString();

It's not especially elegant, but it should give you better control over output encoding. 它不是特别优雅,但它可以让你更好地控制输出编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM