Java tutorial
//package com.java2s; /** * 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(); } }