Here you can find the source of getText(Node nd, StringBuilder buf)
private static void getText(Node nd, StringBuilder buf)
//package com.java2s; /**// w ww .j a v a 2 s . co m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Drop in replacement for Node.getTextContent(). * * <p> * This method is provided to support the same functionality as * Node.getTextContent() but in a way that is DOM Level 2 compatible. * </p> * * @see <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent">DOM Object Model Core</a> */ public static String getText(Node nd) { short type = nd.getNodeType(); // for most node types, we can defer to the recursive helper method, // but when asked for the text of these types, we must return null // (Not the empty string) switch (type) { case Node.DOCUMENT_NODE: /* fall through */ case Node.DOCUMENT_TYPE_NODE: /* fall through */ case Node.NOTATION_NODE: /* fall through */ return null; } StringBuilder sb = new StringBuilder(); getText(nd, sb); return sb.toString(); } /** @see #getText(Node) */ private static void getText(Node nd, StringBuilder buf) { short type = nd.getNodeType(); switch (type) { case Node.ELEMENT_NODE: /* fall through */ case Node.ENTITY_NODE: /* fall through */ case Node.ENTITY_REFERENCE_NODE: /* fall through */ case Node.DOCUMENT_FRAGMENT_NODE: NodeList childs = nd.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); short childType = child.getNodeType(); if (childType != Node.COMMENT_NODE && childType != Node.PROCESSING_INSTRUCTION_NODE) { getText(child, buf); } } break; case Node.ATTRIBUTE_NODE: /* fall through */ /* Putting Attribute nodes in this section does not exactly match the definition of how textContent should behave according to the DOM Level-3 Core documentation - which specifies that the Attr's children should have their textContent concated (Attr's can have a single child which is either Text node or an EntityRefrence). In practice, DOM implementations do not seem to use child nodes of Attributes, storing the "text" directly as the nodeValue. Fortunately, the DOM Spec indicates that when Attr.nodeValue is read, it should return the nodeValue from the child Node, so this approach should work both for strict implementations, and implementations actually encountered. */ case Node.TEXT_NODE: /* fall through */ case Node.CDATA_SECTION_NODE: /* fall through */ case Node.COMMENT_NODE: /* fall through */ case Node.PROCESSING_INSTRUCTION_NODE: /* fall through */ buf.append(nd.getNodeValue()); break; case Node.DOCUMENT_NODE: /* fall through */ case Node.DOCUMENT_TYPE_NODE: /* fall through */ case Node.NOTATION_NODE: /* fall through */ default: /* :NOOP: */ } } }