Writing Russian in XML
- by zavié
Hi,
I am writing a Xml Tag Renamer class with Java which reads in a XML, renames the tags and write them back into another XML file using DocumentBuilderFactory and TransformerFactory (text nodes are preserved). It worked fine before with German and English texts, until today, when I tried to rename a XML file with russian text. Instead of the source texts I got ????? in the newly created XML file. I've tried setting Encoding
Any idea how to correct this?
Thanks!
PS. Strings were correct before entering TransformerFactory, as I checked in the debugger. I've tried setting OutputKeys.ENCODING to UTF-8 and ISO-8859-5. None of them helped.
The Transformer part:
// Output the XML
// Set up a transformer
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
// Fix to a bug about indent in transformer
transformer.setOutputProperty
("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// TODO encoding parameter
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// Create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
xmlString.replaceAll("\n", System.getProperty("line.separator"));
// Write to file
BufferedWriter output = new BufferedWriter(new FileWriter(outputPath));
output.write(xmlString);
output.close();