Java XML Node Text Value getTextContent(final Node node, final StringBuffer sb)

Here you can find the source of getTextContent(final Node node, final StringBuffer sb)

Description

Internally used by getTextContent(Node node) method.

License

Open Source License

Parameter

Parameter Description
node a parameter
sb a parameter

Exception

Parameter Description
DOMException an exception

Declaration

private static void getTextContent(final Node node, final StringBuffer sb) throws DOMException 

Method Source Code


//package com.java2s;
import org.w3c.dom.DOMException;

import org.w3c.dom.Node;

public class Main {
    /**/*  w ww  .j  a v  a 2 s.  c om*/
     * Implemntation of getTextContent to cater the lack of such API in 1.4.2 environment.
     * @param node
     * @return
     * @throws DOMException
     */
    public static String getTextContent(final Node node) throws DOMException {
        String textContent = "";

        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            textContent = node.getNodeValue();
        } else {
            Node child = node.getFirstChild();

            if (child != null) {
                Node sibling = child.getNextSibling();

                if (sibling != null) {
                    StringBuffer sb = new StringBuffer();

                    getTextContent(node, sb);
                    textContent = sb.toString();
                } else {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        textContent = child.getNodeValue();
                    } else {
                        textContent = getTextContent(child);
                    }
                }
            }
        }

        return textContent;
    }

    /**
     * Internally used by getTextContent(Node node) method.
     * @param node
     * @param sb
     * @throws DOMException
     */
    private static void getTextContent(final Node node, final StringBuffer sb) throws DOMException {
        Node child = node.getFirstChild();

        while (child != null) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                sb.append(child.getNodeValue());
            } else {
                getTextContent(child, sb);
            }

            child = child.getNextSibling();
        }
    }
}

Related

  1. getTextBetween(Node node1, Node node2)
  2. getTextBuffer(Node e, StringBuilder sb)
  3. getTextContent(final Node node)
  4. getTextContent(final Node node)
  5. getTextContent(final Node node, final String defaultValue)
  6. getTextContent(final Node xmlNode)
  7. getTextContent(Node baseNode)
  8. getTextContent(Node e)
  9. getTextContent(Node element)