Java XML Element Get by Name getElement(Element parent, String localname, int position)

Here you can find the source of getElement(Element parent, String localname, int position)

Description

Get the child element having the supplied localname, position and namespace.

License

Open Source License

Parameter

Parameter Description
parent Parent element to be searched.
localname Localname of the element required.
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 &ltinput> 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.

Declaration

public static Element getElement(Element parent, String localname, int position) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w . jav 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
 */

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

public class Main {
    /**
     * Get the child element having the supplied localname, position
     * and namespace.
     * <p/>
     * 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 &ltinput&gt; 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.
     * <p/>
     * 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 &ltinput&gt; 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 elements = getElements(parent, localname, namespaceURI);

        position = Math.max(position, 1);
        if (position > elements.size()) {
            return null;
        }

        return (Element) elements.get(position - 1);
    }

    /**
     * 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. getElement(Document document, String elementName)
  2. getElement(Document owner, String elementName, String nsURI)
  3. getElement(Document parent, String name)
  4. getElement(Element parent, String elementName)
  5. getElement(Element parent, String elementName)
  6. getElement(Element parent, String tagName)
  7. getElement(Element parent, String tagName)
  8. getElement(Element parentElement, String nodeName)
  9. getElement(Node parent, int index)