Here you can find the source of getTextContent(Element element)
Parameter | Description |
---|---|
element | The element whose text content is desired. |
public static String getTextContent(Element element)
//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 v a 2s.c o m*/ Obtain the text content of an element. @param element The element whose text content is desired. @return If the element has no text or CDATA children, the value is an empty string. Otherwise, the value is the string representation of all the text and CDATA children, with leading and trailing whitespace removed. **/ public static String getTextContent(Element element) { return getTextContent(element, true); } /** Obtain the text content of an element. @param element The element whose text content is desired. @param trim Whether leading and trailing whitespace should be removed. @return If the element has no text or CDATA children, the value is an empty string. Otherwise, the value is the string representation of all the text and CDATA children. **/ public static String getTextContent(Element element, boolean trim) { String result = null; NodeList nodes = element.getChildNodes(); if (nodes.getLength() == 0) { result = ""; } else { StringBuffer buf = new StringBuffer(64); Node node; int nNodes = nodes.getLength(); int i = -1; while (++i < nNodes) { node = nodes.item(i); short nodeType = node.getNodeType(); if (nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE) { String text = node.getNodeValue(); buf.append(text); } } result = buf.toString(); if (trim) { result = result.trim(); } } return result; } }