Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDOM, umaluts and UTF-8

Tags:

java

utf-8

jdom

I have issue with using utf-8 with JDOM parser. My code won't write encoded in utf-8. Here's code:

import java.io.FileWriter;
import java.io.IOException;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class Jdom2 {

  public static void main(String[] args) throws JDOMException, IOException {

    String textValue = null;
    try {
        SAXBuilder sax = new SAXBuilder();
        Document doc = sax.build("path/to/file.xml");

        Element rootNode = doc.getRootElement();
        Element application = rootNode.getChild("application");
        Element de_DE = application.getChild("de_DE");
        textValue = de_DE.getText();
        System.out.println(textValue);
        application.getChild("de_DE").setText(textValue);

        Format format = Format.getPrettyFormat();
        format.setEncoding("UTF-8");
        XMLOutputter xmlOutput = new XMLOutputter(format);
        xmlOutput.output(doc, new FileWriter("path/to/file.xml"));

    } catch (IOException io) {
        io.printStackTrace();
    } catch (JDOMException e) {
        e.printStackTrace();
    }
  } 
}

So line System.out.println(textValue); prints text "Mit freundlichen Grüßen", which is exactly what is read from xml file, but when it writing down, I get "Mit freundlichen Gr����"

Here's my xml file

<?xml version="1.0" encoding="UTF-8"?>
<applications>
  <application id="default">
    <de_DE>Mit freundlichen Grüßen</de_DE>
  </application>
</applications>

How will I achieve writing "Mit freundlichen Grüßen" instead of "Mit freundlichen Gr����" ? Thanks in advance!

like image 412
user696847 Avatar asked Dec 13 '12 10:12

user696847


1 Answers

This is probably the issue:

xmlOutput.output(doc, new FileWriter("path/to/file.xml"));

FileWriter always uses the platform default encoding. If that's not UTF-8, you've got problems.

I suggest you use a FileOutputStream instead, and let JDOM do the right thing. (Also, I strongly suspect you should keep a reference to the stream so you can close it in a finally block...)

like image 112
Jon Skeet Avatar answered Sep 19 '22 23:09

Jon Skeet