Get the next XML element - Android XML

Android examples for XML:XML Element

Description

Get the next XML element

Demo Code


import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main{
    /**/*  ww  w  .  j  a v a  2 s . c o m*/
     * Get the next element
     * @param element current element
     * @return the element or null if none is found
     */
    public static Element getNextSiblingElement(Element element) {
        Node node = element.getNextSibling();

        if (node != null) {
            while (node.getNodeType() != Node.ELEMENT_NODE) {
                node = node.getNextSibling();

                if (node == null) {
                    return null;
                }
            }
        }

        return (Element) node;
    }
    /**
     * Get the next element with a given name
     * @param element the elements to search
     * @param name the array of names to search for
     * @return the element or null if none is found
     */
    public static Element getNextSiblingElement(Element element, String name) {
        Element elem = getNextSiblingElement(element);

        if (elem != null) {
            while (!elem.getNodeName().equals(name)) {
                elem = getNextSiblingElement(elem);

                if (elem == null) {
                    return null;
                }
            }
        }

        return elem;
    }
    /**
     * Get the next element which equals one of the names in a list
     * @param element the elements to search
     * @param name the array of names to search for
     * @return the element or null if none is found
     */
    public static Element getNextSiblingElement(Element element,
            String[] names) {
        Element elem = getNextSiblingElement(element);

        if (elem != null) {
            while (!Filtering.isIn(names, element.getNodeName())) {
                elem = getNextSiblingElement(elem);

                if (elem == null) {
                    return null;
                }
            }
        }

        return elem;
    }
}

Related Tutorials