Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    public static void printNodeList(NodeList list) {
        for (int i = 0; i < list.getLength(); i++) {
            printNode(list.item(i), "");
            System.out.println("############################");
        }
    }

    public static void printNode(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.print(indent);

        if (nodeName.equals("#text")) {
            String nodeText = node.getNodeValue();

            if ((nodeText != null) && (nodeText.trim().length() > 0)) {
                System.out.print(nodeText.trim());
            }
        } else {
            System.out.print(nodeName);
        }

        System.out.print(" ");

        if (!nodeName.equals("#text")) {
            NamedNodeMap attrs = node.getAttributes();

            if (attrs != null) {
                for (int i = 0; i < attrs.getLength(); i++) {
                    Node attr = attrs.item(i);
                    System.out.print(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\" ");
                }
            }
        }

        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            System.out.println();
            printNode(children.item(i), indent + "\t");
        }
    }
}