Java tutorial
//package com.java2s; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Main { private static String rootElementName; static Document doc = null; public static Document buildDocumentWithRootElement() { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.newDocument(); Element rootElement = doc.createElement(rootElementName); doc.appendChild(rootElement); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return doc; } }