Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;

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

public class Main {
    public static Node findNodeByAttribute(Document document, String tagName, String attributeName,
            String attributeValue) {
        Node foundNode = null;
        NodeList nodes = document.getElementsByTagName(tagName);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            for (int j = 0; j < node.getAttributes().getLength(); j++) {
                Node attribute = node.getAttributes().item(j);
                if (attribute.getNodeName().equals(attributeName)
                        && attribute.getNodeValue().equals(attributeValue)) {
                    foundNode = node;
                    break;
                }
            }
            if (foundNode != null)
                break;
        }
        return foundNode;
    }
}