Java XML Element Sibling getNextSibling(Element node, String name)

Here you can find the source of getNextSibling(Element node, String name)

Description

Get the next sibling element with the given name.

License

Apache License

Parameter

Parameter Description
node the current node for which to get the next sibling
name the name of the next sibling element to get; may be null or "*" to indicate the next sibling of any name

Return

the sibling, or null if none was found

Declaration

public static Element getNextSibling(Element node, String name) 

Method Source Code

//package com.java2s;
/* $HeadURL::                                                                            $
 * $Id$//from  www .ja  v a  2s.co m
 *
 * Copyright (c) 2006-2008 by Topaz, Inc.
 * http://topazproject.org
 *
 * Licensed 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 org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Get the next sibling element with the given name.  This differs from {@link
     * org.w3c.dom.Node#getNextSibling Node.getNextSibling} in that this only returns elements.
     *
     * @param node   the current node for which to get the next sibling
     * @param name   the name of the next sibling element to get; may be null or "*" to indicate the
     *               next sibling of any name
     * @return the sibling, or null if none was found
     */
    public static Element getNextSibling(Element node, String name) {
        final String filter = (name != null && !name.equals("*")) ? name : null;

        for (Node n = node.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
                return (Element) n;
        }

        return null;
    }
}

Related

  1. countPrecedingSiblingsOfType(Element inElem)
  2. getNextElementSibling(Element aElement)
  3. getNextSibling(Element element)
  4. getNextSibling(Element h2)
  5. getNextSiblingElement(Element el)
  6. getNextSiblingElement(Element elem)
  7. getNextSiblingElement(Element elem)
  8. getNextSiblingElement(Element element)