fork download
import java.util.*;
import java.lang.*;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

class Main
{
	public static void main(String[] args) throws ParserConfigurationException, IOException, TransformerException
	{
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
		Document document = documentBuilder.newDocument();
		Element root = document.createElement("root");
		document.appendChild(root);

		Element date = document.createElement("date");
		date.setTextContent("9:51 AM 10/10/2012");
		root.appendChild(date);

		Element responseTime = document.createElement("responseTime");
		responseTime.setTextContent("1.20");
		root.appendChild(responseTime);
		
		Element emp1 = document.createElement("employee");
		Element emp1name = document.createElement("name");
		emp1name.setTextContent("Mohammad");
		emp1.appendChild(emp1name);
		Element emp2 = document.createElement("employee");
		Element emp2name = document.createElement("name");
		emp2name.setTextContent("Ali");
		emp2.appendChild(emp2name);
		
		root.appendChild(emp1);
		root.appendChild(emp2);
		
		printDocument(document, System.out);
	}
	
	public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
	    TransformerFactory tf = TransformerFactory.newInstance();
	    Transformer transformer = tf.newTransformer();
	    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
	    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
	    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
	    transformer.setOutputProperty("{http://x...content-available-to-author-only...e.org/xslt}indent-amount", "4");

	    transformer.transform(new DOMSource(doc), 
	         new StreamResult(new OutputStreamWriter(out, "UTF-8")));
	}
}
Success #stdin #stdout 0.1s 246144KB
stdin
Standard input is empty
stdout
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <date>9:51 AM 10/10/2012</date>
    <responseTime>1.20</responseTime>
    <employee>
        <name>Mohammad</name>
    </employee>
    <employee>
        <name>Ali</name>
    </employee>
</root>