Java XML Element Get by Name getElements(Element parent, String localname, String namespaceURI)

Here you can find the source of getElements(Element parent, String localname, String namespaceURI)

Description

Get the child elements having the supplied localname and namespace.

License

Open Source License

Parameter

Parameter Description
parent Parent element to be searched.
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(Element parent, String localname, String namespaceURI) 

Method Source Code


//package com.java2s;
/*//from  www  .j  a  v  a2s. c  o m
 * 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. getElementByTagName(Element parent, String elementName)
  2. getElementByTagNameWithParents(Element n, String... elementName)
  3. getElementDateValue(Document document, Element parent, String string)
  4. getElementFloatValue(Document document, Element parent, String element)
  5. getElements(Element parent, String localName)
  6. getElements(Element parent, String tagName)
  7. getElements(Element parent, String tagName)
  8. getElements(Element parent, String tagName)
  9. getElements(Element parentElement, String nodeName)