Java XML Element Get Value getTextValue(final Element elem)

Here you can find the source of getTextValue(final Element elem)

Description

get Text Value

License

Open Source License

Declaration

public static String getTextValue(final Element elem) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;

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

import com.google.common.collect.Lists;

public class Main {
    public static String getTextValue(final Element elem) {
        if (elem == null) {
            return null;
        }/*w  ww. j  a v a2  s  . co m*/

        final StringBuilder str = new StringBuilder();
        final NodeList list = elem.getChildNodes();

        for (int i = 0; i < list.getLength(); i++) {
            final Node item = list.item(i);
            if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
                str.append(item.getNodeValue());
            }
        }

        return str.toString();
    }

    public static List<Node> getChildNodes(final Node node) {
        final NodeList list = node.getChildNodes();
        final List<Node> result = Lists.newArrayList();

        for (int i = 0; i < list.getLength(); i++) {
            final Node child = list.item(i);
            result.add(child);
        }

        return result;
    }
}

Related

  1. getTextValue(Element element, String tagName)
  2. getTextValue(Element node)
  3. getTextValue(Element valueEle)
  4. getTextValue(Element valueEle)
  5. getTextValue(Element valueEle)
  6. getTextValue(final Element element, final String tagName)
  7. getTextWithoutTrim(Element elem)
  8. getTrimmedText(Element element)
  9. getTrimmedTextContent(Element element)