dom4j写文件出错

 用dom4j写xml文件出错
public class Log4mw {

    public static final String FILE_NAME = "ESEECONMW_DATA";

    public void createXML(String time, String str, String types) {

        try {
            
            File tmpFile = new File(FILE_NAME);
            //创建目录
            if (!tmpFile.exists()) {
                tmpFile.mkdir();
            }
            File file = new File(FILE_NAME + "/ESEECONMW_Log_" + getDate()
                    + ".xml");
            Element root = null;

            //当天的日志文件不存在
            if (!file.exists()) {

                Document doc = DocumentHelper.createDocument();
                root = doc.addElement("ESEECONMW");
            }
            //文件存在
            else {

                SAXReader reader = new SAXReader();
                Document doc = reader.read(file);
                root = doc.getRootElement();
            }

            Element log = root.addElement("Log");
            //log.addAttribute("time", time);
            Element now = log.addElement("Time");
            now.addText(time);
            
            Element string = log.addElement("String");
            string.addText(str);

            Element type = log.addElement("Type");
            type.addText(types);

            //通过 OutputFormat 来设置XML文档输出格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            format.setSuppressDeclaration(true);
            format.setIndent(true);
            format.setIndent("   ");
            format.setNewlines(true);

            //通过 XMLWriter 创建XML文档(写)
            XMLWriter xmlWriter = new XMLWriter(new FileWriter(file), format);
            xmlWriter.processingInstruction("xml version=\"1.0\"",
                    "encoding=\"UTF-8\"");
            xmlWriter.write(root);
            xmlWriter.close();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取当前日期
     * 
     * @return String
     */
    private String getDate() {

        Calendar cal = Calendar.getInstance();
        String today = cal.get(Calendar.YEAR) + "-"
                + (cal.get(Calendar.MONTH) + 1) + "-"
                + cal.get(Calendar.DAY_OF_MONTH);
        return today;
    }
xml文件的结果,会多出一个 </ESEECONMW> ,为什么? 
<?xml version="1.0" encoding="UTF-8"?>

<ESEECONMW>
   <Log>
      <Time>17:0:36</Time>
      <String>000x</String>
      <Type>null-COM5(Send To Reader)</Type>
   </Log>
</ESEECONMW>
</ESEECONMW>
 
 

你可能感兴趣的:(xml)