Java XML Node Move moveDown(final Node currentN)

Here you can find the source of moveDown(final Node currentN)

Description

move Down

License

Apache License

Declaration

public static void moveDown(final Node currentN) 

Method Source Code

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

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    public static void moveDown(final Node currentN) {
        Node nextSibling = findNextElement(currentN, false);
        Node nextNextSibling = findNextElement(nextSibling, false);
        Node parent = currentN.getParentNode();
        parent.removeChild(currentN);//from   ww  w . j a v a  2  s. c o  m
        if (nextNextSibling != null) {
            parent.insertBefore(currentN, nextNextSibling);
        } else {
            parent.appendChild(currentN);
        }

    }

    public static Element findNextElement(final Node current, final boolean sameName) {
        String name = null;
        if (sameName) {
            name = current.getNodeName();
        }
        int type = Node.ELEMENT_NODE;
        return (Element) getNext(current, name, type);
    }

    public static Node getNext(final Node current, final boolean sameName) {
        String name = null;
        if (sameName) {
            name = current.getNodeName();
        }
        int type = current.getNodeType();
        return getNext(current, name, type);
    }

    public static Node getNext(final Node current, final String name, final int type) {
        Node next = current.getNextSibling();
        if (next == null) {
            return null;
        }

        for (Node node = next; node != null; node = node.getNextSibling()) {
            if (type >= 0 && node.getNodeType() != type) {
                continue;
            } else {
                if (name == null) {
                    return node;
                }
                if (name.equals(node.getNodeName())) {
                    return node;
                }
            }
        }
        return null;
    }
}

Related

  1. moveNodeDown(Node node)
  2. moveNodeUp(Node node)