Java XML Node Sibiling getFirstElementSibling(Node node)

Here you can find the source of getFirstElementSibling(Node node)

Description

Returns the first subsequent sibling of a given node which is an Element.

License

LGPL

Parameter

Parameter Description
node the node whose siblings (including itself) you are interested in. May be <tt>null</tt>

Return

the first sibling of node which is an Element. If node itself is an element, that is returned. If node has no subsequent siblings which are elements, or if it is null, then null is returned.

Declaration

public static Element getFirstElementSibling(Node node) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import org.w3c.dom.*;

public class Main {
    /**//from w  w  w .java  2s.  c  om
     * Returns the first subsequent sibling of a given node which is an Element.
     * This is useful for naviating a DOM as a tree of elements when
     * the presence of text or attribute children is a distraction.
     *
     * @param  node  the node whose siblings (including itself) you are
     *         interested in.  May be <tt>null</tt>
     * @return the first sibling of <tt>node</tt> which is an Element.
     *         If <tt>node</tt> itself is an element, that is returned.
     *         If <tt>node</tt> has no subsequent siblings which are 
     *         elements, or if it is <tt>null</tt>,
     *         then <tt>null</tt> is returned.
     */
    public static Element getFirstElementSibling(Node node) {
        return (node == null || node instanceof Element) ? (Element) node
                : getFirstElementSibling(node.getNextSibling());
    }
}

Related

  1. containsSiblings(org.w3c.dom.Node node)
  2. getFirstSiblingElmt(Node aNode)
  3. getFirstSiblingNamed(Node node, String name)
  4. getLocHomoSibling(Node aNode)
  5. getNextElementSibling(Node node)