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

import org.w3c.dom.Element;

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

public class Main {
    /**
     * return the Vector of child Elements with given local name
     * @param el
     * @param name
     * @return
     */
    public static Vector<Element> namedChildElements(Element el, String lName) {
        Vector<Element> nc = new Vector<Element>();
        for (Iterator<Element> it = childElements(el).iterator(); it.hasNext();) {
            Element en = it.next();
            if (getLocalName(en).equals(lName))
                nc.addElement(en);
        }
        return nc;
    }

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

    /**
     * get the name of an XML element, with the namespace prefix stripped off
     * @param el
     * @return
     */
    public static String getLocalName(Node el) {
        String locName = "";
        StringTokenizer st = new StringTokenizer(el.getNodeName(), ":");
        while (st.hasMoreTokens())
            locName = st.nextToken();
        return locName;
    }
}