Java XML Element Get Value getText(Element e)

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

Description

Gets the text value of the given element.

License

Open Source License

Parameter

Parameter Description
e the element.

Return

the text contents of the given element.s

Declaration

public static String getText(Element e) 

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 ww  w .  j a  va 2  s  .c  o m*/
     * Gets the text value of the given element.
     * 
     * @param e the element.
     * @return the text contents of the given element.s
     */
    public static String getText(Element e) {
        StringBuilder sb = null;

        if (e != null) {
            NodeList children = e.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node node = children.item(i);

                switch (node.getNodeType()) {
                case Node.TEXT_NODE:
                case Node.CDATA_SECTION_NODE:
                    if (sb == null) {
                        sb = new StringBuilder();
                    }
                    sb.append(node.getNodeValue());
                    break;
                }
            }
        }

        return (sb != null) ? sb.toString() : null;
    }
}

Related

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