Java XML Node Previous getAncestorNode(Node visualNode, String tagName)

Here you can find the source of getAncestorNode(Node visualNode, String tagName)

Description

get Ancestor Node

License

Open Source License

Declaration

static public Node getAncestorNode(Node visualNode, String tagName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2014 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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
 *
 * Contributor:// www .j  a v  a2  s .  c om
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import org.w3c.dom.Node;

public class Main {
    static public Node getAncestorNode(Node visualNode, String tagName) {
        if (tagName == null)
            return null;
        Node element = visualNode;
        while (true) {
            if (tagName.equalsIgnoreCase(element.getNodeName())) {
                return element;
            }
            element = element.getParentNode();
            if (element == null) {
                break;
            }
        }
        return null;
    }
}

Related

  1. countElementsBefore(Node node, String tagName)
  2. countElementsBefore(Node node, String tagName)
  3. getAncesters(Node node)
  4. getAncestor(Node node, String ancestorName)
  5. getAncestors(Node node)
  6. getPrevious(final Node current, final boolean sameName)
  7. getPrevious(Node node)
  8. getPreviousComment(Node element)