Java XML Element Sibling getPrecedingSiblings(Element element, String namespaceUri)

Here you can find the source of getPrecedingSiblings(Element element, String namespaceUri)

Description

Returns all preceding sibling elements of an element that belong to a certain namespace.

License

Apache License

Parameter

Parameter Description
element The parent element.
namespaceUri The namespace that the childen must belong to.

Return

The preceding sibling elements.

Declaration

public static Element[] getPrecedingSiblings(Element element, String namespaceUri) 

Method Source Code

//package com.java2s;
/*/*from  w ww.j  av a 2  s.  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * Returns all preceding sibling elements of an element that belong to a
     * certain namespace.
     * @param element The parent element.
     * @param namespaceUri The namespace that the childen must belong to.
     * @return The preceding sibling elements.
     */
    public static Element[] getPrecedingSiblings(Element element, String namespaceUri) {
        return getPrecedingSiblings(element, namespaceUri, "*");
    }

    /**
     * Returns all preceding sibling elements of an element that belong to a
     * certain namespace. and have a certain local name.
     * @param element The parent element.
     * @param namespaceUri The namespace that the childen must belong to.
     * @param localName The local name of the children.
     * @return The preceding sibling elements.
     */
    public static Element[] getPrecedingSiblings(Element element, String namespaceUri, String localName) {
        List childElements = new ArrayList();
        Element parent = (Element) element.getParentNode();
        Element[] children = getChildren(parent, namespaceUri, localName);

        int i = 0;
        while (children[i] != element && i < children.length) {
            childElements.add(children[i]);
            i++;
        }

        return (Element[]) childElements.toArray(new Element[childElements.size()]);
    }

    /**
     * Returns all child elements of an element, regardless of the namespace.
     * @param element The parent element.
     * @return The child elements.
     */
    public static Element[] getChildren(Element element) {
        List childElements = new ArrayList();
        NodeList children = element.getElementsByTagName("*");

        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getParentNode() == element) {
                childElements.add(children.item(i));
            }
        }

        return (Element[]) childElements.toArray(new Element[childElements.size()]);
    }

    /**
     * Returns all child elements of an element that belong to a certain
     * namespace.
     * @param element The parent element.
     * @param namespaceUri The namespace that the childen must belong to.
     * @return The child elements.
     */
    public static Element[] getChildren(Element element, String namespaceUri) {
        return getChildren(element, namespaceUri, "*");
    }

    /**
     * Returns all child elements of an element that belong to a certain
     * namespace and have a certain local name.
     * @param element The parent element.
     * @param namespaceUri The namespace that the childen must belong to.
     * @param localName The local name of the children.
     * @return The child elements.
     */
    public static Element[] getChildren(Element element, String namespaceUri, String localName) {
        List childElements = new ArrayList();
        NodeList children = element.getElementsByTagNameNS(namespaceUri, localName);

        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getParentNode() == element) {
                childElements.add(children.item(i));
            }
        }

        return (Element[]) childElements.toArray(new Element[childElements.size()]);
    }
}

Related

  1. getNextSiblingElement(Element element)
  2. getNextSiblingElement(Element element)
  3. getNextSiblingElement(Node node)
  4. getNextSiblingElementByTagName(Element e, String name)
  5. getNextSiblingElementWithName(Element elem, String name)
  6. getPreviousSiblingElement(Element elem)
  7. getPreviousSiblingElementByTagName(Element e, String name)
  8. getPrevSibling(Element e)
  9. getPrevSibling(Element element)