Java XML Node Path pathUp(Node node, int level)

Here you can find the source of pathUp(Node node, int level)

Description

calculates the path of the node up in the hierarchy, example of result is project/build/plugins/plugin level parameter designates the number of parents to climb eg.

License

Open Source License

Declaration

public static String pathUp(Node node, int level) 

Method Source Code

//package com.java2s;
/**//from ww  w  .  j a  v  a2s .  c o  m
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

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

public class Main {
    /**
     * calculates the path of the node up in the hierarchy, example of result is
     * project/build/plugins/plugin level parameter designates the number of
     * parents to climb eg. for level 2 the result would be plugins/plugin level
     * -1 means all the way to the top.
     */
    public static String pathUp(Node node, int level) {
        StringBuffer buf = new StringBuffer();

        int current = level;

        while ((node != null) && (current > 0)) {
            if (node instanceof Element) {
                if (buf.length() > 0) {
                    buf.insert(0, "/");
                }

                buf.insert(0, node.getNodeName());
                current = current - 1;
            }

            node = node.getParentNode();
        }

        return buf.toString();
    }
}

Related

  1. getVectorPathFromNode(Node node)
  2. getVectorPathFromNode(Node node)
  3. getVectorPathFromNode(Node node)
  4. pathTo(Node xmlNode)
  5. pathUp(Node node, int level)