Java XML Node Tree dumpTree(Node node)

Here you can find the source of dumpTree(Node node)

Description

dump Tree

License

Apache License

Declaration

public static String dumpTree(Node node) 

Method Source Code

//package com.java2s;
/*/*from w  w  w .j av  a 2s  .c  om*/
 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * Licensed 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.
 *
 * Contributors:
 *     Max Stepanov
 *
 * $Id$
 */

import java.util.Stack;

import org.w3c.dom.Node;

public class Main {
    public static String dumpTree(Node node) {
        StringBuilder sb = new StringBuilder();
        Stack<Node> stack = new Stack<Node>();
        int level = 0;
        while (node != null || !stack.isEmpty()) {
            if (node == null) {
                do {
                    node = stack.pop();
                    --level;
                } while (node == null && !stack.isEmpty());
                continue;
            }
            for (int i = 0; i < level; ++i) {
                sb.append(' ');
            }
            sb.append(node.getNodeName()).append(" <").append(node.getNodeValue()).append(">\n");
            stack.push(node.getNextSibling());
            node = node.getFirstChild();
            ++level;
        }
        return sb.toString();
    }
}

Related

  1. dump(final Node node)
  2. dump(Node node)
  3. moveSubTree(Node from, Node to, Node context)