Answer the question
In order to leave comments, you need to log in
How to fix encoding when saving xml file in Java?
Hello brothers! Today I stumbled upon an incomprehensible problem. I migrated my Java project from maven to graddle. And everything is fine, but for some reason XML files are saved in the wrong encoding... When you save them in UTF-8, this is what happens:
<?xml version="1.0" encoding="utf-8"?>
<sky>
<mechanism name="33">
<abilities>
<ability name="�����������1" />
<ability name="22" />
</abilities>
</mechanism>
</sky>
<?xml version="1.0" encoding="windows-1251"?>
<sky>
<mechanism name="33">
<abilities>
<ability name="Проверка" />
<ability name="22" />
</abilities>
</mechanism>
</sky>
public void save(String path) throws IOException
{
Document document = new Document();
document.setRootElement(getXMLElement());
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat().setEncoding("windows-1251")); //utf-8 здесь работает неверно
outputter.output(document, new FileWriter(path));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question