Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * Retrieve all child elements of the given DOM element that match
     * the given element getName. Only look at the direct child level of the
     * given element; do not go into further depth (in contrast to the
     * DOM API's <code>getElementsByTagName</code> method).
     * @param ele the DOM element to analyze
     * @param childEleName the child element getName to look for
     * @return a List of child <code>org.w3c.dom.Element</code> instances
     * @see org.w3c.dom.Element
     * @see org.w3c.dom.Element#getElementsByTagName
     */
    public static List<Node> getChildElements(Element ele, String childEleName) {
        NodeList nl = ele.getChildNodes();
        List<Node> childEles = new ArrayList<Node>();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element && nodeNameEquals(node, childEleName))
                childEles.add(node);
        }
        return childEles;
    }

    /**
     * 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());
    }
}