Java tutorial
//package com.java2s; //License from project: Apache License 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 { public static Document createDocument(String mainType, String customType) throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); document.setXmlStandalone(false); Element sync = document.createElement("Sync"); document.appendChild(sync); Element entity = document.createElement("Entity"); entity.setAttribute("mainType", mainType); if (customType.length() > 0) entity.setAttribute("customType", customType); document.getElementsByTagName("Sync").item(0).appendChild(entity); return document; } }