¿Qué tal algo como esto?
package test;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String args[]) throws Exception {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("test.xml"));
StringBuffer buffer = new StringBuffer();
appendChildren(buffer, doc.getChildNodes());
System.out.println(buffer.toString());
}
private static void appendChildren(StringBuffer buffer, NodeList list) {
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeValue() != null) {
if (node.getNodeValue().trim().length() > 0) {
buffer.append(node.getNodeValue()<