Java XML Element Get Value getText(Element elem)

Here you can find the source of getText(Element elem)

Description

Get Text Content of the given element.

License

Open Source License

Parameter

Parameter Description
elem Element

Return

Trimmed text content of the element.

Declaration

public static final String getText(Element elem) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//from w  w  w.  ja v  a 2s.  c  o m
     * Get Text Content of the given element.
     * 
     * @param elem
     *            Element
     * 
     * @return Trimmed text content of the element.
     */
    public static final String getText(Element elem) {
        if (elem != null) {
            NodeList childNodes = elem.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
                    return trim(childNodes.item(i).getNodeValue());
                }
            }
        }

        return null;
    }

    private static String trim(String input) {
        if (input == null) {
            return input;
        }

        return input.trim();

    }
}

Related

  1. getText(Element e)
  2. getText(Element el)
  3. getText(Element elem)
  4. getText(Element elem)
  5. getText(Element elem)
  6. getText(Element elem, String ifnull)
  7. getText(Element elem, String name)
  8. getText(Element element)
  9. getText(Element element)