Java XML Element Get Value getText(Element elem)

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

Description

get Text

License

Open Source License

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 {
    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());
                }/*  ww w .j a v  a2s  .  com*/
            }
        }

        return null;
    }

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

        return input.trim();

        // int len = input.length();
        //
        // int index = 0;
        //
        // for(index = 0;index < len;index++)
        // {
        // if(!Character.isWhitespace(input.charAt(index)))
        // {
        // break;
        // }
        // }
        //
        // return (index == 0) ? input : input.substring(index);
    }
}

Related

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