阅读java org.w3c.dom.ls的文档它似乎只能将元素序列化为带有java本机字符串编码的字符串UTF-16。但是,我需要创建一个UTF-8字符串,转义或不存在,我知道它仍然是一个UTF-16字符串。任何人都有想法绕过这个? 我需要将字符串传递给将使用String的生成的WS客户端,然后它应该是UTF-8。
我用来创建字符串的代码:
DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.
DOMImplementationLS domImplementationLS = (DOMImplementationLS) REGISTRY.getDOMImplementation("LS");
LSSerializer writer = domImplementationLS.createLSSerializer();
String result = writer.writeToString(element);
答案 0 :(得分:17)
您仍然可以使用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();
答案 1 :(得分:7)
我发现将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();
它不是特别优雅,但它可以让你更好地控制输出编码。