Java XML Element Get Value getText(final Element element, final StringBuilder sbuf, final boolean decend)

Here you can find the source of getText(final Element element, final StringBuilder sbuf, final boolean decend)

Description

Get the text content of an element.

License

Open Source License

Parameter

Parameter Description
element The element.
sbuf The buffer to append to.
decend Whether to descend into child elements.

Declaration

public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) 

Method Source Code

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

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

public class Main {
    /**/* w ww .  j  a  v a 2  s .  c  o  m*/
     * Get the direct text content of an element.
     *
     * @param element
     *            The element.
     *
     * @return The contained text.
     */
    public static String getText(final Element element) {
        final StringBuilder sbuf = new StringBuilder();

        getText(element, sbuf, false);

        return sbuf.toString();
    }

    /**
     * Get the text content of an element.
     *
     * @param element
     *            The element.
     * @param sbuf
     *            The buffer to append to.
     * @param decend
     *            Whether to descend into child elements.
     */
    public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) {
        Node node = element.getFirstChild();

        while (node != null) {
            switch (node.getNodeType()) {
            case Node.TEXT_NODE:
                sbuf.append(node.getNodeValue());
                break;

            case Node.ELEMENT_NODE:
                if (decend) {
                    getText((Element) node, sbuf, decend);
                }

                break;
            }

            node = node.getNextSibling();
        }
    }
}

Related

  1. getText(Element element, String name)
  2. getText(Element element, String tagName)
  3. getText(Element node)
  4. getText(final Element el)
  5. getText(final Element element)
  6. getText(final Element tag)
  7. getTextContent(Element e)
  8. getTextContent(Element e, String name)
  9. getTextContent(Element el)