你好 运行出错不知道怎么解决?
package com.imooc.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class HrWriter { public static void writeXml() { File file=new File("C:\\eclipse2\\1.xml\\bin\\Hr.xml"); SAXReader reader=new SAXReader(); try { Document doc=reader.read(file); Element root=doc.getRootElement(); Element employee=root.addElement("employee"); employee.addAttribute("no", "13"); employee.addElement("name").setText("Demon"); employee.addElement("age").setText("23"); employee.addElement("salary").setText("8000"); Element department=employee.addElement("department"); department.addElement("dname").setText("人事部"); department.addElement("address").setText("西湖区"); Writer writer=new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); doc.write(writer); writer.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { writeXml(); } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 人力资源管理系统 --> <hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Hr.xsd"> <employee no="11"> <name>小明</name> <age>22</age> <salary>3000</salary> <department> <dname>前端</dname> <address>西湖区</address> </department> </employee> <employee no="12"> <name>小强</name> <age>25</age> <salary>80000</salary> <department> <dname>全栈</dname> <address>西湖区</address> </department> </employee> </hr>
org.dom4j.DocumentException: 2 字节的 UTF-8 序列的字节 2 无效。
at org.dom4j.io.SAXReader.read(SAXReader.java:464)
at org.dom4j.io.SAXReader.read(SAXReader.java:246)
at com.imooc.dom4j.HrWriter.writeXml(HrWriter.java:17)
at com.imooc.dom4j.HrWriter.main(HrWriter.java:37)
2
收起
正在回答 回答被采纳积分+1
1回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星