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 {
    /**
     * Utility method that returns the first child element
     * identified by its getName.
     * @param ele the DOM element to analyze
     * @param childName the child element getName to look for
     * @return the <code>org.w3c.dom.Element</code> creating,
     * or <code>null</code> if none found
     */
    public static Element getChildElement(Element ele, String childName) {
        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element && nodeNameEquals(node, childName))
                return (Element) node;
        }
        return null;
    }

    /**
     * Namespace-aware equals comparison. Returns <code>true</code> if either
     * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>,
     * otherwise returns <code>false</code>.
     */
    public static boolean nodeNameEquals(Node node, String desiredName) {
        assert node != null : "Node must not be null";
        assert desiredName != null : "Desired getName must not be null";
        return desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName());
    }
}