Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * 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:
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import org.w3c.dom.*;

public class Main {
    /**
     * Get the path to a given element. The path is a simplified version of XPath. It begins with the first element UNDER
     * the document element.
     * 
     * @param elem Element of interest
     * @return XPath-like path to the element
     */
    public static String getElementPath(Element elem) {
        if (elem == null)
            return null;
        StringBuffer sb = new StringBuffer();
        Node parent = null;
        for (Node node = elem; node != null; node = parent) {
            parent = node.getParentNode();
            if ((node instanceof Element) && (parent instanceof Element)) {
                if (sb.length() > 0)
                    sb.insert(0, '/');
                sb.insert(0, node.getNodeName());
            }
        }
        return sb.toString();
    }
}