Get the child elements having the supplied localname and namespace. - Java XML

Java examples for XML:XML Element Child

Description

Get the child elements having the supplied localname and namespace.

Demo Code

/*/*from  w w  w .  j a v  a 2 s  . 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
 */
//package com.java2s;
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 Tutorials