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 {
    /**
     * Extract the text content of a named node from a list of nodes.
     * Returns null if the node is not found.
     *
     * @param nodeList a list of nodes
     * @param nodeName name of the node to extract from the list
     * @return found node text or null if not found
     */
    public static String getNodeTextFromList(final NodeList nodeList, final String nodeName) {

        String text = null;

        if (nodeList != null) {
            final Node node = getNodeFromList(nodeList, nodeName);

            if (node != null) {
                text = node.getTextContent();
            }
        }

        return text;
    }

    /**
     * Extract a named node from a list of nodes.
     * Note that if more than one instance of the named node
     * exists, then the returned node will be undefined and could
     * be any of the nodes that match.
     *
     * @param nodeList a list of nodes
     * @param nodeName name of the node to extract from the list
     * @return found node or null if not found
     */
    public static Node getNodeFromList(final NodeList nodeList, final String nodeName) {

        Node node = null;

        if (nodeList != null) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                if (nodeName.equalsIgnoreCase(nodeList.item(i).getNodeName())) {
                    node = nodeList.item(i);
                }
            }
        }

        return node;
    }
}