Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {

    public static Element getElementByTagNameAndAttributeValue(Element element, String tagName, String attrName,
            String attrValue) {
        NodeList nodeList = element.getElementsByTagName(tagName);
        if (nodeList.getLength() == 0) {
            return null;
        }
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element nodeElement = (Element) node;
                if (attrValue.equals(getAttributeValueByName(nodeElement, attrName))) {
                    return nodeElement;
                }
            }
        }
        return null;
    }

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