Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// License as published by the Free Software Foundation; either

import org.w3c.dom.Element;

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

public class Main {
    /**
     * Looks up a child with the given (local) name.
     * <p>
     * An array of several names may be passed, in which case they will be traversed in a simple
     * XPath-like fashion.
     */

    public static Element getChildNamed(Element element, String... names) {

        if (element == null) {
            return null;
        }

        Element child = null;
        NodeList children = element.getChildNodes();

        outer: for (String name : names) {
            for (int loop = 0, length = children.getLength(); loop < length; loop++) {
                Node node = children.item(loop);

                if (!(node instanceof Element)) {
                    continue;
                }

                child = (Element) node;

                if (name.equals(child.getLocalName())) {
                    children = child.getChildNodes();
                    continue outer;
                }
            }

            // No match found

            return null;
        }

        return child;
    }
}