Java XML Node Sibiling getNextSiblingElement(Node node)

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

Description

get Next Sibling Element

License

Apache License

Declaration

public static Node getNextSiblingElement(Node node) 

Method Source Code

//package com.java2s;
/*//w ww.  j a  va 2s.  c om
 * Copyright (c) 2010-2013 Evolveum
 *
 * 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.Attr;

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

public class Main {
    public static Node getNextSiblingElement(Node node) {
        if (node == null || node.getParentNode() == null) {
            return null;
        }
        Node parent = node.getParentNode();
        NodeList nodes = parent.getChildNodes();
        if (nodes == null) {
            return null;
        }
        boolean found = false;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.equals(node)) {
                found = true;
                continue;
            }
            if (found && child.getNodeType() == Node.ELEMENT_NODE) {
                return child;
            }
        }

        return null;
    }

    private static Node getParentNode(Node node) {
        if (node instanceof Attr) {
            Attr attr = (Attr) node;
            return attr.getOwnerElement();
        } else {
            return node.getParentNode();
        }
    }
}

Related

  1. getNextSiblingElement(Node node)
  2. getNextSiblingElement(Node node)
  3. getNextSiblingElement(Node node)
  4. getNextSiblingElement(Node node)
  5. getNextSiblingElement(Node node)
  6. getNextSiblingElement(Node node)
  7. getNextSiblingElement(Node node)
  8. getNextSiblingElement(Node node, String elemNames[])
  9. GetNextSiblingElement(Node start)