@Override public void saveAllTasks(Collection<Task> tasks) { try { doc = new Document(new Element("tasks"), new DocType("tasks", "XmlDTD.dtd")); Iterator it = tasks.iterator(); while (it.hasNext()) { Task task = (Task) it.next(); doc.getRootElement() .addContent( new Element("task") .setAttribute("id", task.getId()) .addContent(new Element("name").addContent(task.getName())) .addContent(new Element("description").addContent(task.getDescription())) .addContent(new Element("date").addContent(sdf.format(task.getDate())))); } XMLOutputter outPutter = new XMLOutputter(Format.getRawFormat().setIndent(" ").setLineSeparator("\n")); outPutter.output(doc, new FileWriter(config.getFileName())); } catch (IOException ex) { log.error(null, ex); throw new WritingFileException("Didn't write xml", ex.getCause()); } }