Start a new XML Document : DOM Document « XML « Java






Start a new XML Document

    
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class Utils {

  /**
   * Start a new XML Document.
   * @param rootName The name of the Document root Element (created here)
   * @return the Document
   * @throws DomException
   */
  public static Document createXmlDocument(String rootName)  {

      Document  document  = getXmlDocumentBuilder().newDocument();
      Element   root      = document.createElement(rootName);

      document.appendChild(root);
      return document;

  
  }
  /**
   * Get a DOM Document builder.
   * @return The DocumentBuilder
   * @throws DomException
   */
  public static DocumentBuilder getXmlDocumentBuilder() {
    try {
      DocumentBuilderFactory factory;

      factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(false);

      return factory.newDocumentBuilder();

    } catch (Exception e) {
      
    }
    return null;
  }

}

   
    
    
    
  








Related examples in the same category

1.Copy an XML document
2.Create DOM Document out of string
3.Create Document with root QName
4.Create Empty DOM Document
5.Displays a DOM document in a tree control.
6.New Document From InputStream
7.New Document From String
8.load Document by element
9.load Document from InputStream
10.get Document Element from a file
11.Document To String
12.Utility class to print out DOM
13.Return a new document, ready to populate
14.Read Xml from InputStream and return Document
15.Read Xml from Reader and return Document
16.Gets the owner document of a node.
17.Creates an element on the given document.
18.Loads a W3C XML document from a file.
19.Your Own XML Reader