Java XML Node Previous getPreviousElementNode(Node node)

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

Description

get Previous Element Node

License

Open Source License

Declaration

private static Node getPreviousElementNode(Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2009 IBM Corporation and others.
 * 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
 * /* ww  w.jav a 2 s. c o m*/
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.w3c.dom.Text;

public class Main {
    private static Node getPreviousElementNode(Node node) {
        Node previous = node.getPreviousSibling();

        while (!(previous instanceof Element) && previous != null) {
            previous = previous.getPreviousSibling();
        }
        if (previous instanceof Text) {
            return null;
        }
        return previous;
    }
}

Related

  1. getAncestors(Node node)
  2. getPrevious(final Node current, final boolean sameName)
  3. getPrevious(Node node)
  4. getPreviousComment(Node element)
  5. getPreviousComment(Node element)
  6. getPreviousNodeByName(Node currentNode, String tagName)
  7. getPreviousTypedNode(Node node, short nodeType)
  8. getPreviousTypedNode(Node node, short nodeType)
  9. getPreviousTypedNode(Node node, short nodeType)