Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

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

public class Main {
    /**
     * add multiple elements to a node. it doesn't check if an element exists with the same name.
     * @param contextNode is the node to which the element is added
     * @param elmName is the new element name, not XPath.
     * @param elmValues are the text values of the elements.
     */
    public static void addElements(Node contextNode, String elmName, String elmValues[]) throws Exception {
        if (elmValues == null)
            return;
        for (int i = 0; i < elmValues.length; ++i)
            addSingleElement(contextNode, elmName, elmValues[i]);
    }

    /**
     * add multiple elements to a node. it doesn't check if an element exists with the same name.
     * @param contextNode is the node to which the element is added
     * @param elmName is the new element name, not XPath.
     * @param elmValues are the text values of the elements.
     */
    public static void addElements(Node contextNode, String elmName, List<?> elmValues) throws Exception {
        if (elmValues == null)
            return;
        for (int i = 0; i < elmValues.size(); ++i)
            addSingleElement(contextNode, elmName, (String) elmValues.get(i));
    }

    /**
     * add single element to a node. it doesn't check if an element exists with the same name.
     * @param contextNode is the node to which the element is added
     * @param elmName is the new element name, not XPath.
     * @return the new Element
     */
    public static Element addSingleElement(Node contextNode, String elmName) throws Exception {
        Element elm = contextNode.getOwnerDocument().createElement(elmName);
        contextNode.appendChild(elm);
        return (elm);
    }

    /**
     * add single element to a node. it doesn't check if an element exists with the same name.
     * @param contextNode is the node to which the element is added
     * @param elmName is the new element name, not XPath.
     * @param elmValue is the text value of the element.
     * @return the new Element
     */
    public static Element addSingleElement(Node contextNode, String elmName, String elmValue) throws Exception {
        Element elm = addSingleElement(contextNode, elmName);
        if (elmValue != null)
            elm.appendChild(contextNode.getOwnerDocument().createTextNode(elmValue));
        return (elm);
    }
}