import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.FactoryConfigurationError;  
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; 

 
import java.io.File;
import java.io.IOException;

import org.w3c.dom.*;



public class DomXform {
    static Document document;
    private static Element makeElementText(String element,String text) {
      Element e = (Element) document.createElement(element);
      e.appendChild(document.createTextNode(text));
      return e;
      }

    public static void makeDOM() {
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder=factory.newDocumentBuilder();
            document=builder.newDocument();
            Element root = 
                (Element) document.createElement("book"); 
            document.appendChild(root);
            root.appendChild(makeElementText("title","Deathworld"));
            root.appendChild(makeElementText("author","Harrison"));
            Element shelving = 
                (Element) document.createElement("shelving");
            root.appendChild(shelving);
            shelving.appendChild(makeElementText("shelve","scifi"));
            shelving.appendChild(makeElementText("shelve","trilogies"));
            shelving.appendChild(makeElementText("shelve","Harrison"));
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }
    static int level=0;

// convert a node type to a string
    public static String nodeType(Node node) {
        switch (node.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            return "Attribute";
        case Node.CDATA_SECTION_NODE:
            return "CDATA Section";
        case Node.COMMENT_NODE:
            return "Comment";
        case Node.DOCUMENT_FRAGMENT_NODE:
            return "Doc Fragment";
        case Node.DOCUMENT_NODE:
            return "Document";
        case Node.DOCUMENT_TYPE_NODE:
            return "Document Type";
        case Node.ELEMENT_NODE:
            return "Element";
        case Node.ENTITY_NODE:
            return "Entity";
        case Node.ENTITY_REFERENCE_NODE:
            return "Entity Reference";
        case Node.NOTATION_NODE:
            return "Notation";
        case Node.PROCESSING_INSTRUCTION_NODE:
            return "Processing Instruction";
        case Node.TEXT_NODE:
            return "Text";
        default:
            return "Unknown";
        }
    }

    public static void main(String argv[]) throws Exception
    {
        try {
// This one line loads the whole document!
           makeDOM();
           // Create a transformer
           TransformerFactory tFactory =
               TransformerFactory.newInstance();
           Transformer transformer = tFactory.newTransformer();

           DOMSource source = new DOMSource(document);
           StreamResult result = new StreamResult(System.out);

           // Write it out
           transformer.transform(source, result);


 
        } catch (Exception e) {
           e.printStackTrace();
        }
    } // main


}
