Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 CA.  All rights reserved.
 *
 * This source file is licensed under the terms of the Eclipse Public License 1.0
 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import org.w3c.dom.Node;

public class Main {
    /**
     * Gets a node value by name
     * 
     * @param parent   the parent node
     * @param nodeName   the node name
     * @return String   the value of the node 
     * @throws Exception
     */
    public static String getChildNodeValueByName(Node parent, String nodeName) throws Exception {
        Node node = findChildNodeByName(parent, nodeName);
        if (node != null && node.getFirstChild() != null) {
            return node.getFirstChild().getNodeValue().trim();
        }

        return "";
    }

    /**
     * Search a child node by name 
     * 
     * @param parent   the parent node
     * @param nodeName   the node name for searching
     * @return         Node with the specified name
     * @see            Node
     * @throws Exception
     */
    public static Node findChildNodeByName(Node parent, String nodeName) throws Exception {

        Node child = null;
        Node node = parent.getFirstChild();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                child = node;
                break;
            }
            node = node.getNextSibling();
        }

        return child;
    }
}