javaencodingutf-8iso-8859-1dom4j

Change encoding of DOM4J Document: UTF to ISO-8859-1 (Java)


I need to create an org.dom4j.Document but when I print it it's always UTF-8.

I want to change it to ISO-8859-1 but I didn't find the way to do it. It's not possible to use .setEncoding() and the Document is created on the fly (not reading from an InputStream).

It's the same problem that is discussed at http://www.coderanch.com/t/127978/XML/change-Encoding-Dom

Thanks a lot!


Solution

  • I believe you can set the encoding in the OutputFormat format class and use that to configure XMLWriter.

    OutputFormat outFormat = new OutputFormat();    
    outFormat.setEncoding("ISO-8859-1");
    
    XMLWriter out = new XMLWriter(outputStream, outFormat);
    out.write(myDocumentObject);
    

    You will need to provide the XMLWriter class an OutputStream or Writer.