Java XML Node Previous getAncesters(Node node)

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

Description

Get a list of ancester nodes starting from the Document till the node.

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

private static List getAncesters(Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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
 *
 * Contributors:/*ww w .  j  av a 2 s  .co m*/
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    /**
     * Get a list of ancester nodes starting from the Document till the node.
     * 
     * @param node
     * @return
     */
    private static List getAncesters(Node node) {
        List list = new ArrayList();
        while (node != null) {
            list.add(node);
            if (node instanceof Document) {
                break;
            }
            node = node.getParentNode();
        }
        if (node == null) {
            // if part ==null, means we didn't find a DocumentEditPart,
            // something must be wrong.
            return null;
        }
        // reverse to make it starting from the docuemnteditpart node.
        Collections.reverse(list);
        list.add(null); // add an null terminator.
        return list;
    }
}

Related

  1. countElementsBefore(Node node, String tagName)
  2. countElementsBefore(Node node, String tagName)
  3. getAncestor(Node node, String ancestorName)
  4. getAncestorNode(Node visualNode, String tagName)
  5. getAncestors(Node node)
  6. getPrevious(final Node current, final boolean sameName)