Java tutorial
//package com.java2s; /* Copyright (c) 2009-2011 Olivier Chafik, All Rights Reserved This file is part of JNAerator (http://jnaerator.googlecode.com/). JNAerator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. JNAerator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with JNAerator. If not, see <http://www.gnu.org/licenses/>. */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static StringBuffer gatherTextPCDATAAndCDATADescendants(Node node, StringBuffer sb, String separator) { if (sb == null) sb = new StringBuffer(); int t = node.getNodeType(); if (t == Node.CDATA_SECTION_NODE || t == Node.TEXT_NODE) { //sb.append("================= "+node.getNodeName()+" ================="); sb.append(node.getNodeValue()); } else { NodeList list = node.getChildNodes(); for (int i = 0, len = list.getLength(); i < len; i++) { Node n = list.item(i); gatherTextPCDATAAndCDATADescendants(n, sb, separator); if (i < len - 1) sb.append(separator); } } return sb; } }