Java XML Node Value getNodeValue(Node nodeXML)

Here you can find the source of getNodeValue(Node nodeXML)

Description

Return the value of the node nodeXML

License

Apache License

Parameter

Parameter Description
nodeXML Node object

Declaration

public static String getNodeValue(Node nodeXML) 

Method Source Code

//package com.java2s;
/*//  w  ww .  ja v  a  2 s  .com
 * 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;
    }
}

Related

  1. getNodeValue(Node node)
  2. getNodeValue(Node node)
  3. getNodeValue(Node node, String name, boolean trim)
  4. getNodeValue(Node node, String name, String defaultVal, int index)
  5. getNodeValue(Node node, String nodeKey)
  6. getNodeValue(org.w3c.dom.Node node)
  7. getNodeValue(org.w3c.dom.Node node)
  8. getNodeValueAsDate(Node node)
  9. getNodeValueAsLong(Node node)