这个主要是你的参数使用问题
XMLWriter writer=new XMLWriter(new FileOutputStream(new File("student.xml"),true),output);
把参数true该为false就可以了 true是append(追加模式),改为false就是覆盖模式了
XMLWriter writer=new XMLWriter(new FileOutputStream(new File("student.xml"),true),output);
把参数true该为false就可以了 true是append(追加模式),改为false就是覆盖模式了
writeDeclaration(); if (doc.getDocType() != null) {
indent();
writeDocType(doc.getDocType());
} for (int i = 0, size = doc.nodeCount(); i < size; i++) {
Node node = doc.node(i);
writeNode(node);
} writePrintln(); if (autoFlush) {
flush();
}
}也就是说每次使用write(Document doc)这个方法时候,都会默认增加xml头,即代码中红色部分。所以你每次执行程序都会添加那一句话。
2.如楼上所说,true为append。运行add(String name,String sex)方法时,若为true,则在原xml文件后面拼接新的xml代码。若为false,则覆盖掉原来的xml代码。