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 the number of element and attribute nodes in an XML tree
     */
    public static int nodeCount(Element el) {
        int count = 1 + el.getAttributes().getLength(); // this node and its attributes

        // contributions from child elements
        Vector<Element> childElements = childElements(el);
        for (int i = 0; i < childElements.size(); i++)
            count = count + nodeCount(childElements.get(i));

        return count;
    }

    /**
     * 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;
    }
}