Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Attr;

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

public class Main {
    public static String logElement(Element El, int level) {

        String Es = "";
        String indentText = "  ";
        String addIndT = "";

        // add indent
        int ind = 0;
        while (ind < level) {
            addIndT = addIndT + indentText;
            ind++;

        }
        String name = El.getNodeName();
        Es = "\n" + addIndT + "<" + name + " ";
        // Attribs
        NamedNodeMap namedNodeMap = El.getAttributes();
        StringBuilder sb = new StringBuilder(Es);
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            Attr att = (Attr) namedNodeMap.item(i);
            sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" ");
            // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() +
            // "\" ";
        }
        sb.append(">");
        // Es = Es + ">";
        Es = sb.toString();
        NodeList pl = El.getChildNodes();
        int index = pl.getLength();
        // text nodes
        if (index > 0) {
            int i = 0;
            while (i < index) {
                Node DomNode = pl.item(i);
                if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                    String Etext = DomNode.getNodeValue();
                    Es = Es + "\n " + addIndT + addIndT + Etext;
                }
                i++;
            }
        }
        // Child Elements
        if (index > 0) {
            level++;
            int i = 0;
            while (i < index) {
                Node DomNode = pl.item(i);
                if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                    Element el = (Element) DomNode;
                    Es = Es + logElement(el, level);
                }
                i++;
            }
        }
        Es = Es + "\n" + addIndT + "</" + name + ">";

        return Es;
    }
}