Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I need however to create a UTF-8 string, escaped or what not, I understand that it still will be a UTF-16 String. 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.

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);
like image 264
Tomas Avatar asked Oct 28 '09 11:10

Tomas


2 Answers

You can still use 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();
like image 69
Jeryl Cook Avatar answered Oct 05 '22 04:10

Jeryl Cook


I find that the most flexible way of serializing a DOM to String is to use the 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.

like image 20
skaffman Avatar answered Oct 05 '22 04:10

skaffman