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 java.util.StringTokenizer;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * @param el
     * @return the number of element and attribute nodes in an XML tree
     */
    public static int nodeValueCount(Element el) {
        int count = el.getAttributes().getLength(); //  attributes of this node

        // text value of this node
        String text = getText(el);
        if ((text != null) && (!text.equals("")))
            count++;

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

        return count;
    }

    /**
     * get the  text string in an element (eg interspersed between child elements), 
     * or "" if there is none or if the Element is null.
     * Tries to ignore white space text; but does not succeed.
     */
    public static String getText(Element el) {
        String res = "";
        if (el != null)
            try {
                el.normalize(); // does not help recognise white space
                NodeList nodes = el.getChildNodes();
                for (int i = 0; i < nodes.getLength(); i++)
                    if (nodes.item(i) instanceof Text) {
                        Text text = (Text) nodes.item(i);
                        // this filter seems to make no difference
                        if (!text.isElementContentWhitespace()) {
                            String tData = text.getData();
                            // this seems to be an effective way to catch pure white space
                            StringTokenizer nonWhiteSpace = new StringTokenizer(tData, "\n \t");
                            if (nonWhiteSpace.countTokens() > 0)
                                res = res + tData;
                        }
                    }
            } catch (Exception e) {
                System.out.println("Text failure: " + e.getMessage());
            }
        return res;
    }

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