按照方力勋视频写的为什么出现乱码了呢?通过test()方法写入的数据没有问题,但是每次xml写入前的代码却出现了乱码问题!
求帮忙解决。- <?xml version="1.0" encoding="UTF-8"?>
- <exam>
- <student examid="3433" idcard="4210811993">
- <name>张三</name>
- <location>上海</location>
- <grade>60</grade>
- </student>
- <student examid="20998241" idcard="12321">
- <name>李四</name>
- <location>武汉</location>
- <grade>23</grade>
- </student>
- <student examid="98567" idcard="45689">
- <name>lucy</name>
- <location>USA</location>
- <grade>23</grade>
- </student>
- </exam>
复制代码- public static void test() throws Exception{
- SAXReader reader = new SAXReader();
- Document document = reader.read(new FileReader("src/exam.xml"));
- Element root=document.getRootElement();
- Element examNode=root.addElement("student");
- examNode.setAttributeValue("examid", "23456");
- examNode.setAttributeValue("idcard", "658987");
- examNode.addElement("name").setText("翟凌");
- examNode.addElement("location").setText("北京");
- examNode.addElement("grade").setText("89");
-
-
- OutputFormat format = OutputFormat.createPrettyPrint();
- format.setEncoding("UTF-8");
- XMLWriter writer = new XMLWriter(new FileOutputStream("src/exam.xml"),format);
- writer.write(document);
- writer.close();
- }
复制代码 |