Java XML Node Value getNodeValue(List elements, String nodeName, String defaultValue)

Here you can find the source of getNodeValue(List elements, String nodeName, String defaultValue)

Description

Return the text value of the first child of the named node, or the specified default if the node can't be found.
For example, calling getNodeValue(el, "Mode", "whatever") on a list of elements which contains the following XML: survival should return "survival".

License

Open Source License

Parameter

Parameter Description
elements the list of XML elements to search
nodeName the name of the parent node to extract the text value from
defaultValue the default to return if the node is empty / doesn't exist

Return

the text content of the desired element

Declaration

static public String getNodeValue(List<Element> elements, String nodeName, String defaultValue) 

Method Source Code

//package com.java2s;
//  sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is

import java.util.List;

import org.w3c.dom.Element;

public class Main {
    /** Return the text value of the first child of the named node, or the specified default if the node can't be found.<br>
     * For example, calling getNodeValue(el, "Mode", "whatever") on a list of elements which contains the following XML:
     *  <Mode>survival</Mode>
     * should return "survival".//from  w  w  w.  j  av  a2 s  . c  om
     * @param elements the list of XML elements to search
     * @param nodeName the name of the parent node to extract the text value from
     * @param defaultValue the default to return if the node is empty / doesn't exist
     * @return the text content of the desired element
     */
    static public String getNodeValue(List<Element> elements, String nodeName, String defaultValue) {
        if (elements != null) {
            for (Element el : elements) {
                if (el.getNodeName().equals(nodeName)) {
                    if (el.getFirstChild() != null) {
                        return el.getFirstChild().getTextContent();
                    }
                }
            }
        }
        return defaultValue;
    }
}

Related

  1. getNodeVal(Node node)
  2. getNodeValue(final Node iNode)
  3. getNodeValue(final Node node)
  4. getNodeValue(final Node node)
  5. getNodeValue(Node aNode)
  6. getNodeValue(Node fatherNode, String nodeName)
  7. getNodeValue(Node iNode)
  8. getNodeValue(Node iNode)