Java XML Element Get Value getTextContent(Element el)

Here you can find the source of getTextContent(Element el)

Description

Returns a string representing the plain text content of an element.

License

LGPL

Parameter

Parameter Description
el the element whose text content is wanted

Return

the pure text content. If there is none, an empty string is returned.

Declaration

public static String getTextContent(Element el) 

Method Source Code


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

import org.w3c.dom.*;

public class Main {
    /**/*from  w  w w.j  a va2 s  . co  m*/
     * Returns a string representing the plain text content of an element.
     * Any comments, attributes, elements or other non-text children 
     * are ignored, and all CDATA and Text nodes are merged to 
     * give a single string.
     * 
     * @param   el  the element whose text content is wanted
     * @return  the pure text content.  If there is none, an empty 
     *          string is returned.
     */
    public static String getTextContent(Element el) {
        StringBuffer sb = new StringBuffer();
        for (Node child = el.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child instanceof Text) {
                Text childText = (Text) child;
                sb.append(childText.getData());
            }
        }
        return sb.toString();
    }
}

Related

  1. getText(final Element element)
  2. getText(final Element element, final StringBuilder sbuf, final boolean decend)
  3. getText(final Element tag)
  4. getTextContent(Element e)
  5. getTextContent(Element e, String name)
  6. getTextContent(Element ele)
  7. getTextContent(Element element)
  8. getTextContent(Element element)
  9. getTextContent(Element element)