开发者

Producing UTF-8 encoded XML in Java

开发者 https://www.devze.com 2023-04-09 06:04 出处:网络
This is the code I\'m using try { String str = \"\\uC3BC and \\uC3B6 and <&> für\"; DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

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 &lt;&amp;&gt; für"/>

I want it to output

attribute="&#xc3bc and &#xc3b6 ..."

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号