|
DOM4J writes garbled files, and re-encoding does not work. I wrote a test code as follows
import java.io. *;
import org.dom4j. *;
import org.dom4j.io. *;
public class Test {
public static void main (String [] args)
{
try {
SAXReader reader = new SAXReader ();
Document document = reader.read ("c: /Demo.txt");
Element root = document.getRootElement ();
String a = "R & D department";
Element newElement = root.addElement ("Department")
.addAttribute ("value", new String (a.getBytes (), "UTF-8"))
;
// OutputFormat format = new OutputFormat ("", true, "GBK");
// Using format can solve the problem. However, XML is specified as UTF-8.
XMLWriter writer = new XMLWriter (
new FileOutputStream (new File ("c: /Demo.txt")));
// new FileWriter ("c: /Demo.txt"));
// Using FileWriter is not correct, DOM4J is not transcoded, it will report an error when writing the second time
writer.write (document);
writer.close ();
} catch (Exception e) {System.out.println (e.getMessage ());}
}
}
Demo.txt
<? xml version = "1.0" encoding = "UTF-8"?>
<Company>
</ Company> |
|