title: Output format of XML empty element in Java
body: <p>I wrote code below to get XML output.</p>
  
  <pre><code>DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.newDocument();
  
  Element element = document.createElement("Test");
  Text text = document.createTextNode("");
  element.appendChild(text);
  document.appendChild(element);
  
  TransformerFactory transFactory = TransformerFactory.newInstance();
  Transformer transformer = transFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  
  DOMSource source = new DOMSource(document);
  StreamResult result = new StreamResult(System.out);
  transformer.transform(source, result);
  </code></pre>
  
  <p>What I got is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Test/&gt;
  </code></pre>
  
  <p>What I want to get is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Test&gt;&lt;/Test&gt;
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p>Many thanks.</p>