Java XML Element Value getElementValue(final Element target)

Here you can find the source of getElementValue(final Element target)

Description

returns the text value associated with the element

License

Apache License

Parameter

Parameter Description
target - the element

Return

- the text value

Declaration

public static String getElementValue(final Element target) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;

public class Main {
    /**/*from  w  ww  . jav a  2 s.  c o  m*/
     * returns the text value associated with the element
     *
     * @param target - the element
     * @return - the text value
     */
    public static String getElementValue(final Element target) {

        final NodeList nodeList = target.getChildNodes();
        if (nodeList == null) {
            return null;
        }
        for (int current = 0; current < nodeList.getLength(); current++) {
            final Node node = nodeList.item(current);
            if (node instanceof Text) {
                final Text text = (Text) node;
                final String value = text.getNodeValue();
                if ((value != null) && (!value.isEmpty())) {
                    return value;
                }
            }
        }
        return "";
    }
}

Related

  1. getElementValue(Element element, String tagName)
  2. getElementValue(Element elm)
  3. getElementValue(Element p_element)
  4. getElementValue(Element root, String elemName)
  5. getElementValue(final Element e)