public static void saveToXmlFile(File filename) { try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = document.createElement("data"); document.appendChild(root); Element entry = document.createElement("entry"); entry.setAttribute("name", "Foo Fighters: Back and Forth"); entry.setAttribute("year", "2011"); entry.setAttribute("genre", "Documentary"); root.appendChild(entry); entry = document.createElement("entry"); entry.setAttribute("name", "Bar girls"); entry.setAttribute("year", "1994"); entry.setAttribute("genre", "Drama"); root.appendChild(entry); entry = document.createElement("entry"); entry.setAttribute("name", "Cane-Toad: What Happened to Baz?"); entry.setAttribute("year", "2002"); entry.setAttribute("genre", "Animation/Short"); root.appendChild(entry); printDocument(document, new FileOutputStream(filename)); } catch (ParserConfigurationException | TransformerException | IOException e) { e.printStackTrace(); } }