Java XML Element Sibling getPrevSibling(Element element)

Here you can find the source of getPrevSibling(Element element)

Description

Get the previous sibling element of the specified element, null if it has no previous other siblings.

License

Open Source License

Parameter

Parameter Description
element The element to search siblings for.

Return

The previous sibling element, null if it has none.

Declaration

public static Element getPrevSibling(Element element) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w w w  .j a  v  a  2 s  .co m*/
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import org.w3c.dom.*;

public class Main {
    /**
     * Get the previous sibling element of the specified element, null if it has no previous other siblings.
     * 
     * @param element The element to search siblings for.
     * @return The previous sibling element, null if it has none.
     */
    public static Element getPrevSibling(Element element) {
        if (element == null)
            return null;
        Node node = element.getPreviousSibling();
        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE)
                return (Element) node;
            node = node.getPreviousSibling();
        }
        return null;
    }
}

Related

  1. getNextSiblingElementWithName(Element elem, String name)
  2. getPrecedingSiblings(Element element, String namespaceUri)
  3. getPreviousSiblingElement(Element elem)
  4. getPreviousSiblingElementByTagName(Element e, String name)
  5. getPrevSibling(Element e)
  6. getSiblingElement(Element e, String name)
  7. getSiblingNamed(Element element, String name)