Producing UTF-8 encoded XML in Java
This is the code I'm using
try {
String str = "\uC3BC and \uC3B6 and <&> für";
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("test");
root.setAttribute("attribute", str);
doc.appendChild(root);
DOMSource domSource = new DOMSource(doc);
// FileOutputStream out = new FileOutputStream("test.xml");
Writer out = new OutputStreamWriter(new FileOutputStream("test.xml"), "UTF8");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(domSource, new StreamResult(out));
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Output is
<?xml version="1.0" encoding="UTF-8" standalone="no"?开发者_运维百科>
<test attribute="쎼 and 쎶 and <&> für"/>
I want it to output
attribute="쎼 and 쎶 ..."
How do I achieve this ?
I'm using Java 1.6-20
This is similar to Producing valid XML with Java and UTF-8 encoding
If you don't want the XML to be encoded as UTF-8, you shouldn't tell the transformer to do so.
If I understand your question correctly
transformer.setOutputProperty(OutputKeys.ENCODING, "US-ASCII");
should produce the output that you want
精彩评论