Java XML NodeList getElements(NodeList nodeList, String localname, String namespaceURI)

Here you can find the source of getElements(NodeList nodeList, String localname, String namespaceURI)

Description

Get the child elements having the supplied localname and namespace.

License

Open Source License

Parameter

Parameter Description
nodeList List of DOM nodes on which to perform the search.
localname Localname of the element required. Supports "*" wildcards.
namespaceURI Namespace URI of the required element, or null if a namespace comparison is not to be performed.

Return

A list of W3C DOM OPTION_WITHOUT_PARAMETER. An empty list if no such child elements exist on the parent element.

Declaration

public static List getElements(NodeList nodeList, String localname, String namespaceURI) 

Method Source Code


//package com.java2s;
/*//from   w  ww .j a  v a2  s  .  c  om
 * ePUB Corrector - https://github.com/vysokyj/epub-corrector/
 *
 * Copyright (C) 2012 Jiri Vysoky
 *
 * ePUB Corrector is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * ePUB Corrector is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cobertura; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

import org.w3c.dom.*;
import java.util.List;
import java.util.Vector;

public class Main {
    /**
     * Get the child elements having the supplied localname and namespace.
     * <p/>
     * 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}OPTION_WITHOUT_PARAMETER.  An empty list if no such
     *         child elements exist on the parent element.
     */
    public static List getElements(Element parent, String localname, String namespaceURI) {

        return getElements(parent.getChildNodes(), localname, namespaceURI);
    }

    /**
     * Get the child elements having the supplied localname and namespace.
     * <p/>
     * 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}OPTION_WITHOUT_PARAMETER.  An empty list if no such
     *         child elements exist on the parent element.
     */
    public static List getElements(NodeList nodeList, String localname, String namespaceURI) {

        int count = nodeList.getLength();
        Vector elements = new Vector();

        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.
     * <p/>
     * Returns the {@link Node#getLocalName() localName} of the element
     * if set (namespaced element), otherwise the
     * element'OPTION_WITHOUT_PARAMETER {@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();
        }
    }
}

Related

  1. getDSNameListInScriptText(String scriptText, NodeList tableList)
  2. getElementById(NodeList nodeList, String id)
  3. getElementFromItem(NodeList list, int index)
  4. getElementFromNodeList(NodeList nl)
  5. getElementList(NodeList list)
  6. getElementsFromNodeList(final NodeList nodeList)
  7. getElementsOfNodeList(NodeList nodeList)
  8. getEnvVariableMap(NodeList nodes)
  9. getFirst(NodeList nodes)