Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

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

public class Main {

    public static Element[] getChildrenByAttrubte(Element parent, String attrName, String attrValue) {
        NodeList nodes = parent.getChildNodes();
        int len = nodes.getLength();
        Element temp;
        List<Element> list = new ArrayList<Element>(len);
        if (nodes != null && len > 0) {
            for (int i = 0; i < len; i++) {
                if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    temp = (Element) nodes.item(i);
                    if (getAttribute(temp, attrName).equalsIgnoreCase(attrValue))
                        list.add(temp);
                }
            }
        }
        return list.toArray(new Element[list.size()]);
    }

    public static String getAttribute(Element elem, String attrName) {
        return elem.getAttribute(attrName);
    }
}