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 returns node value for given child. If there is no text
     * available for given node, then this method returns null
     * 
     * @return Node value of input node
     * @throws IllegalArgumentException
     *             if input is invalid
     */
    public static String getNodeValue(final Node inputNode) {
        // Child count
        int childCount = 0;

        if (inputNode == null) {
            return null;
        }

        // Return null if child not found
        final NodeList childList = inputNode.getChildNodes();
        if ((childList == null) || (childList.getLength() < 1)) {
            return null;
        }

        // Get child count
        childCount = childList.getLength();

        // For each child
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            // Get each child
            final Node childNode = childList.item(childIndex);

            // Check if text node
            if (childNode.getNodeType() == Node.TEXT_NODE) {
                // Return node value
                return childNode.getNodeValue();
            }
        }

        // If no text node found return null
        return null;
    }
}