Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    public static Node findNodeWithAttributeValue(Document d, String name, String attribute, String value) {
        Node body = d.getDocumentElement();

        return actualFindNodeWithAttributeValue(body, name, attribute, value);
    }

    public static Node findNodeWithAttributeValue(Node node, String name, String attribute, String value) {
        return actualFindNodeWithAttributeValue(node, name, attribute, value);
    }

    private static Node actualFindNodeWithAttributeValue(Node node, String name, String attribute, String value) {

        String nodeName = node.getNodeName();
        nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));
        if (nodeName.equals(name)) {
            Element e = (Element) node;
            if (e.getAttribute(attribute) != null && e.getAttribute(attribute).equals(value))
                return node;
        }
        if (node.hasChildNodes()) {
            NodeList list = node.getChildNodes();
            int size = list.getLength();
            for (int i = 0; i < size; i++) {
                Node found = actualFindNodeWithAttributeValue(list.item(i), name, attribute, value);
                if (found != null)
                    return found;
            }
        }
        return null;
    }
}