import java.io.*;
import org.apache.xml.serialize.*;
import org.w3c.dom.*;

public class Test2 {

  public static void main(String[] args) {
    Test2 test = new Test2();
  }

  public Test2() {
        Document doc = new org.apache.xerces.dom.DocumentImpl();
        Element root = doc.createElement("root");
        Element ele1 = doc.createElement("ele1");
        Element ele2 = doc.createElement("ele2");
        ele2.setAttribute("attr1", "attrval1");
        Comment comm = doc.createComment(" This is a comment ");

        ((Node)ele1).appendChild(ele2);
        ((Node)ele1).appendChild(comm);
        ((Node)root).appendChild(ele1);
        ((Node)doc).appendChild(root);

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("test.xml")));
            OutputFormat of = new OutputFormat(doc);
            of.setIndenting(true);
            XMLSerializer xser = new XMLSerializer(pw, of);
            xser.serialize(doc);
        } catch (Exception e) {
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
  }
}