Java XML Node Previous getPrevious(final Node current, final boolean sameName)

Here you can find the source of getPrevious(final Node current, final boolean sameName)

Description

get Previous

License

Apache License

Declaration

public static Node getPrevious(final Node current, final boolean sameName) 

Method Source Code

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

import org.w3c.dom.Node;

public class Main {
    public static Node getPrevious(final Node current, final boolean sameName) {
        String name = null;/*  w  w w  .  j a  v  a2s .  co  m*/
        if (sameName) {
            name = current.getNodeName();
        }

        int type = current.getNodeType();
        return getPrevious(current, name, type);

    }

    public static Node getPrevious(final Node current, final String name, final 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;
    }
}

Related

  1. countElementsBefore(Node node, String tagName)
  2. getAncesters(Node node)
  3. getAncestor(Node node, String ancestorName)
  4. getAncestorNode(Node visualNode, String tagName)
  5. getAncestors(Node node)
  6. getPrevious(Node node)
  7. getPreviousComment(Node element)
  8. getPreviousComment(Node element)
  9. getPreviousElementNode(Node node)