Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Vector;

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

public class Main {
    public static synchronized Element findElementByAttr(Element e, String attrName, String attrValue) {
        return findElementByAttr(e, attrName, attrValue, true);
    }

    public static synchronized Element findElementByAttr(Element e, String attrName, String attrValue,
            boolean dept) {
        if (e == null || attrName == null || attrName.length() == 0 || attrValue == null
                || attrValue.length() == 0) {
            return null;
        }
        String tmpValue = null;
        Element[] childs = getChildElements(e);
        for (int i = 0; i < childs.length; i++) {
            tmpValue = childs[i].getAttribute(attrName);
            if (attrValue.equals(tmpValue)) {
                return childs[i];
            }
        }
        if (dept) {
            for (int i = 0; i < childs.length; i++) {
                Element retn = findElementByAttr(childs[i], attrName, attrValue);
                if (retn != null) {
                    return retn;
                }
            }
        }
        return null;
    }

    public static synchronized Element[] getChildElements(Element element) {
        if (element == null) {
            return null;
        }
        Vector childs = new Vector();
        for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
            if (node instanceof Element) {
                childs.add((Element) node);
            }
        }
        Element[] elmt = new Element[childs.size()];
        childs.toArray(elmt);
        return elmt;
    }

    public static synchronized Element[] getChildElements(Element element, String childName) {
        if (element == null || childName == null || childName.length() == 0) {
            return null;
        }
        Vector childs = new Vector();
        for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
            if (node instanceof Element) {
                childs.add((Element) node);
            }
        }
        Element[] elmt = new Element[childs.size()];
        childs.toArray(elmt);
        return elmt;
    }

    public static synchronized String getAttribute(Element e, String name, String defvalue) {
        if (e == null || name == null || name.length() == 0) {
            return defvalue;
        } else {
            return e.getAttribute(name);
        }
    }
}