Java XML Document Create createDocument(String rootNodeName)

Here you can find the source of createDocument(String rootNodeName)

Description

This method creates a new Document Object.

License

Apache License

Exception

Parameter Description
Exception an exception

Declaration

public static Document createDocument(String rootNodeName) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   ww  w .  j  a  v  a  2 s.  c  om*/
 * Copyright 2013 Keith D Swenson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris,
 * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava,
 * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi
 */

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

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

public class Main {
    /**
     * This method creates a new Document Object.
     * Pass in the name of the root node, since you ALWAYS need
     * a root node, and attaching this to the document is not like other children.
     * Retrieve the root element with the standard getDocumentElement.
     * @return
     * @throws Exception
     */
    public static Document createDocument(String rootNodeName) throws Exception {
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        dfactory.setNamespaceAware(true);
        dfactory.setValidating(false);
        DocumentBuilder bldr = dfactory.newDocumentBuilder();
        Document doc = bldr.newDocument();
        Element rootEle = doc.createElement(rootNodeName);
        doc.appendChild(rootEle);
        return doc;
    }

    public static Document createDocument(String rootNodeName, String schema) throws Exception {
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        dfactory.setNamespaceAware(true);
        dfactory.setValidating(false);
        DocumentBuilder bldr = dfactory.newDocumentBuilder();
        Document doc = bldr.newDocument();
        Element rootEle = doc.createElement(rootNodeName);
        rootEle.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootEle.setAttribute("xsi:noNamespaceSchemaLocation", schema);
        doc.appendChild(rootEle);
        return doc;
    }
}

Related

  1. createDocument(String pageID)
  2. createDocument(String root, NodeList content)
  3. createDocument(String rootElement)
  4. createDocument(String rootElementName)
  5. createDocument(String rootName)
  6. createDocument(String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID)
  7. createDocument(String str)
  8. createDocument(String xml)
  9. createDocumentBuilder()