fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7.  
  8. import javax.xml.parsers.DocumentBuilder;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10. import javax.xml.parsers.ParserConfigurationException;
  11. import javax.xml.transform.OutputKeys;
  12. import javax.xml.transform.Transformer;
  13. import javax.xml.transform.TransformerException;
  14. import javax.xml.transform.TransformerFactory;
  15. import javax.xml.transform.dom.DOMSource;
  16. import javax.xml.transform.stream.StreamResult;
  17.  
  18. import org.w3c.dom.Document;
  19. import org.w3c.dom.Element;
  20.  
  21. class Main
  22. {
  23. public static void main(String[] args) throws ParserConfigurationException, IOException, TransformerException
  24. {
  25. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  26. DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
  27. Document document = documentBuilder.newDocument();
  28. Element root = document.createElement("root");
  29. document.appendChild(root);
  30.  
  31. Element date = document.createElement("date");
  32. date.setTextContent("9:51 AM 10/10/2012");
  33. root.appendChild(date);
  34.  
  35. Element responseTime = document.createElement("responseTime");
  36. responseTime.setTextContent("1.20");
  37. root.appendChild(responseTime);
  38.  
  39. Element emp1 = document.createElement("employee");
  40. Element emp1name = document.createElement("name");
  41. emp1name.setTextContent("Mohammad");
  42. emp1.appendChild(emp1name);
  43. Element emp2 = document.createElement("employee");
  44. Element emp2name = document.createElement("name");
  45. emp2name.setTextContent("Ali");
  46. emp2.appendChild(emp2name);
  47.  
  48. root.appendChild(emp1);
  49. root.appendChild(emp2);
  50.  
  51. printDocument(document, System.out);
  52. }
  53.  
  54. public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
  55. TransformerFactory tf = TransformerFactory.newInstance();
  56. Transformer transformer = tf.newTransformer();
  57. transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
  58. transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  59. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  60. transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  61. transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
  62.  
  63. transformer.transform(new DOMSource(doc),
  64. new StreamResult(new OutputStreamWriter(out, "UTF-8")));
  65. }
  66. }
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>