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 String idName = "Name";

    public static Element getElement(Element parentElement, String nodeName) {
        NodeList nodeList = parentElement.getElementsByTagName(nodeName);
        if (nodeList.getLength() == 1) {
            Node node = nodeList.item(0);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
        return null;
    }

    public static Element getElement(Element parentElement, String nodeName, String id) {
        NodeList nodeList = parentElement.getElementsByTagName(nodeName);
        int i;
        for (i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if (id.equals(element.getAttribute(idName))) {
                    return element;
                }
            }
        }
        return null;
    }
}