Java XML Node Print printTree(Node node, int ident)

Here you can find the source of printTree(Node node, int ident)

Description

prints the document tree

License

Open Source License

Parameter

Parameter Description
node node to start at
ident amount of indention

Declaration

public static void printTree(Node node, int ident) 

Method Source Code


//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /**prints the document tree
    * @param node node to start at// w w w. ja v a  2 s  .  c o  m
    * @param ident amount of indention*/
    public static void printTree(Node node, int ident) {
        if (node == null)
            return;
        NodeList children;
        System.out.print("Node: " + node.getNodeName() + " ");
        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE:
            System.out.println("Document Node");
            break;

        case Node.ELEMENT_NODE:
            System.out.println("Element Node");
            break;

        case Node.TEXT_NODE:
            System.out.println("->" + node.getNodeValue().trim() + "<-");
            break;

        case Node.CDATA_SECTION_NODE:
            System.out.println("CData Node");
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            System.out.println("Proposing Instruction Node");
            break;

        case Node.ENTITY_REFERENCE_NODE:
            System.out.println("Entity Node");
            break;

        case Node.DOCUMENT_TYPE_NODE:
            System.out.println("Document Node");
            break;

        default:
        }

        for (int j = 0; j < 2 * ident; j++)
            System.out.print(" ");
        System.out.println("It has the following Children");
        children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                for (int j = 0; j < ident; j++)
                    System.out.print(" ");
                System.out.print("Child " + ident + "." + i + " = ");
                printNodeType(children.item(i), ident + 1);
            }
            System.out.println();
        }
    }

    /**prints the type of the input node
    * @param node node to print type of
    * @param ident amount to indent*/
    public static void printNodeType(Node node, int ident) {
        System.out.print("Node: " + node.getNodeName() + " ");
        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE:
            System.out.println("Document Node");
            break;

        case Node.ELEMENT_NODE:
            System.out.println("Element Node");
            for (int j = 0; j < 2 * ident; j++)
                System.out.print(" ");
            System.out.println("It has the following Children");
            NodeList children = node.getChildNodes();
            if (children != null) {
                for (int i = 0; i < children.getLength(); i++) {
                    for (int j = 0; j < ident; j++)
                        System.out.print(" ");
                    System.out.print("Child " + ident + "." + i + " = ");
                    printNodeType(children.item(i), ident + 1);
                }
                System.out.println();
            }
            break;

        case Node.TEXT_NODE:
            System.out.println("->" + node.getNodeValue().trim() + "<-");
            break;

        case Node.CDATA_SECTION_NODE:
            System.out.println("CData Node");
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            System.out.println("Proposing Instruction Node");
            break;

        case Node.ENTITY_REFERENCE_NODE:
            System.out.println("Entity Node");
            break;

        case Node.DOCUMENT_TYPE_NODE:
            System.out.println("Document Node");
            break;

        default:
        }
    }
}

Related

  1. printNodeBasics(Node node)
  2. printNodeIterator(NodeIterator iterator)
  3. printNodes(Node node, int level)
  4. printNodeType(Node node, int ident)
  5. printToTerminal(Node n, String indent, boolean descend)
  6. printTree(Node root, int level)