Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2012 Frederic SACHOT
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Return the value of the node nodeName
     * @param parent parent Node of the node nodeName
     * @param nodeName name of the node
     */
    public static String getNodeValue(Node parent, String nodeName) {
        String value = "";
        if (getNode(parent, nodeName).getChildNodes().item(0) != null) {
            value = getNode(parent, nodeName).getChildNodes().item(0).getNodeValue();
        }
        return value;
    }

    /**
     * Return the value of the node nodeXML
     * @param nodeXML Node object
     */
    public static String getNodeValue(Node nodeXML) {
        return nodeXML.getChildNodes().item(0).getNodeValue();
    }

    /**
     * Return a Node object with the name is nodeName
     * @param parent parent Node which contains the node nodeName
     * @param nodeName name of the node
     * @return a Node object with the name is nodeName<br>
     * if the node parent doesn't have child nodes, null is return.
     */
    public static Node getNode(Node parent, String nodeName) {
        if (parent.hasChildNodes()) {
            for (int i = 0; i < parent.getChildNodes().getLength(); i++) {
                if (parent.getChildNodes().item(i).getNodeName().equalsIgnoreCase(nodeName)) {
                    return parent.getChildNodes().item(i);
                }
            }
        }
        return null;
    }
}