Here you can find the source of getTextContent(final Node node, final StringBuffer sb)
Parameter | Description |
---|---|
node | a parameter |
sb | a parameter |
Parameter | Description |
---|---|
DOMException | an exception |
private static void getTextContent(final Node node, final StringBuffer sb) throws DOMException
//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(); } } }