Here you can find the source of getNodeValue(List
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 |
static public String getNodeValue(List<Element> elements, String nodeName, String defaultValue)
//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; } }