Writing Russian in XML

I am writing a Xml Tag RenamerJava class that reads in XML, renames tags, and writes them back to another XML file using DocumentBuilderFactoryand TransformerFactory(text nodes are saved). It worked perfectly with German and English texts until today, when I tried to rename the XML file with Russian text. Instead of the source code, I got it ?????in a newly created XML file. I tried to set the encoding

Any idea how to fix this?

PS. The lines were correct before injecting TransformerFactory, as I checked in the debugger. I tried installing OutputKeys.ENCODINGon UTF-8and ISO-8859-5. None of them helped.

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();
+3
2

:

transformer.transform(source, new StreamResult(
   new OutputStreamWriter(new FileOutputStream(outputPath), "UTF-8")));
+3

( ) , , . , ( ), , , . , - \u0000 - \u00ff xmlString. , OutputStreamWriter . , , , , , ( , OutputStreamWriter - , ISO8859-1 , ).

XML , . , XML ...

+1

Source: https://habr.com/ru/post/1739683/


All Articles