Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

import java.util.*;

public class Main {
    /**
     * Get the child element having the supplied localname, position
     * and namespace.
     * Can be used instead of XPath.
     * @param parent Parent element to be searched.
     * @param localname Localname of the element required.
     * @param position The position of the element relative to other sibling
     *              elements having the same name (and namespace if specified) e.g. if
     *              searching for the 2nd <input> element, this param needs to have a value of 2.
     * @return The element at the requested position, or null if no such child
     * element exists on the parent element.
     */
    public static Element getElement(Element parent, String localname, int position) {
        return getElement(parent, localname, position, null);
    }

    /**
     * Get the child element having the supplied localname, position
     * and namespace.
     * Can be used instead of XPath.
     * @param parent Parent element to be searched.
     * @param localname Localname of the element required.
     * @param position The position of the element relative to other sibling
     *                  elements having the same name (and namespace if specified) e.g. if
     *                  searching for the 2nd <input> element, this param needs to have a value of 2.
     * @param namespaceURI Namespace URI of the required element, or null
     * if a namespace comparison is not to be performed.
     * @return The element at the requested position, or null if no such child
     * element exists on the parent element.
     */
    public static Element getElement(Element parent, String localname, int position, String namespaceURI) {
        List<Element> elements = getElements(parent, localname, namespaceURI);
        position = Math.max(position, 1);
        if (position > elements.size()) {
            return null;
        }
        return elements.get(position - 1);
    }

    /**
     * Get the child elements having the supplied localname and namespace.
     * Can be used instead of XPath.
     * @param parent Parent element to be searched.
     * @param localname Localname of the element required.  Supports "*" wildcards.
     * @param namespaceURI Namespace URI of the required element, or null
     * if a namespace comparison is not to be performed.
     * @return A list of W3C DOM {@link Element}s.  An empty list if no such
     * child elements exist on the parent element.
     */
    public static List<Element> getElements(Element parent, String localname, String namespaceURI) {
        return getElements(parent.getChildNodes(), localname, namespaceURI);
    }

    /**
     * Get the child elements having the supplied localname and namespace.
     * Can be used instead of XPath.
     * @param nodeList List of DOM nodes on which to perform the search.
     * @param localname Localname of the element required.  Supports "*" wildcards.
     * @param namespaceURI Namespace URI of the required element, or null
     * if a namespace comparison is not to be performed.
     * @return A list of W3C DOM {@link Element}s.  An empty list if no such
     * child elements exist on the parent element.
     */
    public static List<Element> getElements(NodeList nodeList, String localname, String namespaceURI) {
        int count = nodeList.getLength();
        List<Element> elements = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if (localname.equals("*") || getName(element).equals(localname)) {
                    // The local name matches the element we're after...
                    if (namespaceURI == null || namespaceURI.equals(element.getNamespaceURI())) {
                        elements.add(element);
                    }
                }
            }
        }
        return elements;
    }

    /**
     * Get the name from the supplied element.
     * Returns the {@link Node#getLocalName() localName} of the element
     * if set (namespaced element), otherwise the
     * element's {@link Element#getTagName() tagName} is returned.
     * @param element The element.
     * @return The element name.
     */
    public static String getName(Element element) {
        String name = element.getLocalName();
        if (name != null) {
            return name;
        } else {
            return element.getTagName();
        }
    }
}