一个结点对应一行,怎么纳闷了?
授人以渔,不授人以鱼。
public static void write() { try { // 创建一个xml文档 Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root"); //创建一个根元素 root.addAttribute("id", "1"); //为根元素添加一个属性 Element student = root.addElement("student"); //root根元素添加一个子元素student student.addAttribute("id", "2"); //为student添加属性 Element student1 = root.addElement("student"); //root根元素添加一个子元素student student1.addAttribute("id","3");//为student添加属性 File file = new File("src/dom4j-modify.xml"); if (file.exists()) { file.delete(); } file.createNewFile(); XMLWriter out = new XMLWriter(new FileWriter(file)); out.write(doc); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }