Get the previous sibling XML element. - Java XML

Java examples for XML:DOM Element

Description

Get the previous sibling XML element.

Demo Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**/*www .  j a v a  2 s  . co m*/
     * Get the previous sibling element.
     * 
     * @param node The start node.
     * @return The previous sibling element or {@code null}.
     */
    public static Element getPreviousSiblingElement(Node node) {
        Node n = node.getPreviousSibling();
        while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getPreviousSibling();
        }

        return (Element) n;
    }
}

Related Tutorials