Utils.java Source code

Java tutorial

Introduction

Here is the source code for Utils.java

Source

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;
    }

}