Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { public static void moveUp(Node currentN) { Node prevSibling = findPreviousElement(currentN, false); if (prevSibling != null) { Node parent = currentN.getParentNode(); parent.removeChild(currentN); parent.insertBefore(currentN, prevSibling); } } public static Element findPreviousElement(Node current, boolean sameName) { String name = null; if (sameName) { name = current.getNodeName(); } int type = Node.ELEMENT_NODE; return (Element) getPrevious(current, name, type); } public static Node getPrevious(Node current, boolean sameName) { String name = null; if (sameName) { name = current.getNodeName(); } int type = current.getNodeType(); return getPrevious(current, name, type); } public static Node getPrevious(Node current, String name, int type) { Node prev = current.getPreviousSibling(); if (prev == null) return null; for (Node node = prev; node != null; node = node.getPreviousSibling()) { if (type >= 0 && node.getNodeType() != type) { continue; } else { if (name == null) return node; if (name.equals(node.getNodeName())) { return node; } } } return null; } }