Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * This method sets value to given node
     * 
     * @param inputNode
     *            Node to which value needs to be set
     * @param nodeValue
     *            Value to set
     * @throws IllegalArgumentException
     *             if input is invalid
     */
    public static void setNodeTextValue(final Node inputNode, final String nodeValue)
            throws IllegalArgumentException {
        // Child list
        NodeList childList = null;

        // Validate input stream
        if (inputNode == null) {
            throw new IllegalArgumentException("Input Node cannot be null in XmlUtils.setNodeValue");
        }

        // Get child list
        childList = inputNode.getChildNodes();

        // If child nodes found
        if ((childList != null) && (childList.getLength() > 0)) {
            // Get child count
            final int childCount = childList.getLength();

            // For each child
            for (int childIndex = 0; childIndex < childCount; childIndex++) {
                final Node childNode = childList.item(childIndex);
                // Check if text node
                if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
                    // Set value to text node
                    childNode.setNodeValue(nodeValue);
                    break;
                }
            }
        } else {
            // Create text node and set node value
            inputNode.appendChild(inputNode.getOwnerDocument().createTextNode(nodeValue));
        }
    }
}