Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Vector;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * 
     * @param el
     * @return depth of an XML node tree
     */
    public static int getDepth(Element el) {
        int depth = 1;
        if (el.getAttributes().getLength() > 0)
            depth = 2;

        int max = 0;
        Vector<Element> childElements = childElements(el);
        for (int i = 0; i < childElements.size(); i++) {
            int childDepth = getDepth(childElements.get(i));
            if (childDepth > max)
                max = childDepth;
        }

        return depth + max;
    }

    /**
     * Vector of child elements of an element
     */
    public static Vector<Element> childElements(Element el) {
        Vector<Element> res = new Vector<Element>();
        NodeList nodes = el.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node nd = nodes.item(i);
            if (nd instanceof Element) {
                Element eg = (Element) nd;
                res.addElement(eg);
            }
        }
        return res;
    }
}