I wrote an XML parser, everything is working fine except the text encoding. I made some researches to fix this, but i'm still stuck.
I've got a list of string which contains movies titles and I add it to the XML with a CDATA encapsulation, for example :
CDATA movieTitle= new CDATA(aMovie.getTitle());
movie.addContent(new Element("title").addContent(movieTitle));
And I save it using this :
XMLOutputter xmlOutput = new XMLOutputter();
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
xmlOutput.setFormat(format);
xmlOutput.output(doc, new FileWriter(fileName+ ".xml"));
But the result is :
<title><![CDATA[LA LOI DU MARCHxC9]></title>
And should be "LA LOI DU MARCHÉ".
What should I do to avoid this happening ?
As the XML already knows about the encoding, and places it in the
<?xml encoding ?>
, I prefer the solution of @rolfl, a binary OutputStream.The error here is, that FileWriter is a very old utility class that uses the default encoding. Which is absolutely non-portable.