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 {
    /**
     * Get the first found child element with the provided local name that is a
     * direct child the provided element.
     * 
     * @param parent
     *            The parent element
     * @param name
     *            The name of the child element to find
     * @return The first found child element, null if no matching children
     */
    public static Element getFirstChildElementByLocalName(Element parent, String localName) {
        assertNotNull(parent);
        NodeList children = parent.getChildNodes();
        Node node;
        for (int i = 0; i < children.getLength(); i++) {
            node = children.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(localName)) {
                return (Element) node;
            }
        }

        return null;
    }

    /**
     * Performs a check that the provided Nodeobject is not null.
     * 
     * @param node
     *            The node to check
     * @throws IllegalArgumentException
     *             In case the node is null
     */
    private static void assertNotNull(Node node) {
        if (node == null) {
            throw new IllegalArgumentException("The input node cannot be null");
        }
    }
}