Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /**prints the document tree
     * @param node node to start at
     * @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:
        }
    }
}