Java XML Node Value getNodeValue(Node fatherNode, String nodeName)

Here you can find the source of getNodeValue(Node fatherNode, String nodeName)

Description

Gets a node's text value given its parent node and the name of the wanted child node.

License

Apache License

Parameter

Parameter Description
fatherNode the parent node
nodeName name of the wanted child node

Return

String text node value

Declaration

public static String getNodeValue(Node fatherNode, String nodeName) 

Method Source Code

//package com.java2s;
/**//w w  w.  j  av  a2  s.c  om
 * Copyright 2008 - 2009 Pro-Netics S.P.A.
 *
 *    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.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Gets a node's text value given its parent node and the name of the wanted child node.<br>
     * 
     * @param fatherNode the parent node
     * @param nodeName name of the wanted child node
     * @return String text node value
     */
    public static String getNodeValue(Node fatherNode, String nodeName) {
        String value = null;
        if (((Element) fatherNode).getElementsByTagName(nodeName) != null) {
            Node node = ((Element) fatherNode).getElementsByTagName(nodeName).item(0);
            value = node.getTextContent();
            if (value != null) {
                value = value.trim();
            }
        }
        return value;
    }

    /**
     * Gets the node value
     * 
     * @param node Node 
     * @return String node value
     */
    public static String getNodeValue(Node node) {
        String value = null;
        if (node.getFirstChild() != null) {
            value = node.getFirstChild().getNodeValue();
        }
        return value;
    }
}

Related

  1. getNodeValue(final Node iNode)
  2. getNodeValue(final Node node)
  3. getNodeValue(final Node node)
  4. getNodeValue(List elements, String nodeName, String defaultValue)
  5. getNodeValue(Node aNode)
  6. getNodeValue(Node iNode)
  7. getNodeValue(Node iNode)
  8. getNodeValue(Node N)
  9. getNodeValue(Node n)