Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Given an element, returns all its direct child elements that have the
     * specified namespace and name.  Use null to indicate the empty namespace.
     */
    public static List<Element> getElementsNS(Element element, String namespaceURI, String localName) {
        List<Element> elements = new ArrayList<>();
        for (Element candidate : getElements(element)) {
            if (namespaceURI.equals(candidate.getNamespaceURI()) && localName.equals(candidate.getLocalName())) {
                elements.add(candidate);
            }
        }
        return elements;
    }

    /** Returns all the direct child elements of the given element. */
    public static List<Element> getElements(Element element) {
        List<Element> elements = new ArrayList<>();
        for (Node node : toIterable(element.getChildNodes())) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) node);
            }
        }
        return elements;
    }

    /** Converts a NodeList to an Iterable of Nodes. */
    public static Iterable<Node> toIterable(NodeList nodeList) {
        List<Node> nodes = new ArrayList<>(nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            nodes.add(nodeList.item(i));
        }
        return nodes;
    }
}