Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String printDom(Node n, String prefix) {
        StringBuilder sb = new StringBuilder();
        printDom(n, prefix, sb);
        return sb.toString();
    }

    /**
     * Prints out the DOM tree.
     * 
     * @param n
     *            the parent node to start printing from
     * @param prefix
     *            string to append to output, should not be null
     */
    public static void printDom(Node n, String prefix, StringBuilder sb) {
        String outString = prefix;
        if (n.getNodeType() == Node.TEXT_NODE) {
            outString += "'" + n.getTextContent().trim() + "'";
        } else {
            outString += n.getNodeName();
        }
        sb.append(outString);
        sb.append("\n");
        NodeList children = n.getChildNodes();
        int length = children.getLength();
        for (int i = 0; i < length; i++) {
            printDom(children.item(i), prefix + "  ", sb);
        }
    }
}