Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

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

public class Main {
    /**
     * Appends a child element to the provided parent element.
     * This child element is created with the name and integer value provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @param value the integer value to be assigned to the created child element
     * @return the newly created child element 
     */
    public static Element appendChildElement(final Element parentElement, final String name, final int value) {
        return appendChildElement(parentElement, name, "" + value);
    }

    /**
     * Appends a child element to the provided parent element.
     * This child element is created with the name and boolean value provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @param value the boolean value to be assigned to the created child element
     * @return the newly created child element
     */
    public static Element appendChildElement(final Element parentElement, final String name, final boolean value) {
        return appendChildElement(parentElement, name, value ? "true" : "false");
    }

    /**
     * Appends an empty child element to the provided parent element.
     * This child element is created with the name provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @return the newly created empty child element 
     */
    public static Element appendChildElement(final Element parentElement, final String name) {
        return appendChildElement(parentElement, name, null);
    }

    /**
     * Appends a child element to the provided parent element.
     * This child element is created with the name and string value provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @param value the string value to be assigned to the created child element
     * @return the newly created child element 
     */
    public static Element appendChildElement(final Element parentElement, final String name, final String value) {
        Document parentDocument = parentElement.getOwnerDocument();
        Element createElement = createElement(parentDocument, name);
        if (value != null) {
            createElement.appendChild(parentDocument.createTextNode(value));
        }
        parentElement.appendChild(createElement);
        return createElement;
    }

    private static Element createElement(final Document parentDocument, final String name) {
        return parentDocument.createElement(name);
    }
}