Java tutorial
//package com.java2s; /* * RapidMiner * * Copyright (C) 2001-2013 by Rapid-I and the contributors * * Complete list of developers available at our web site: * * http://rapid-i.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Stack; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Returns the XPath to retrieve targetElement from rootElement. rootElement may be null, in this case the XPath starts with and includes * the farthest non-null ancestor of targetElement. If rootElement == targetElement, an empty string * is returned. * @param includeElementIndex Indicates if the element indices in the form elementName[n] should * be included in the XPath. * @param namespacesMap Maps namespace ids to namespace URIs. */ public static String getXPath(Element rootElement, Element targetElement, boolean includeElementIndex, Map<String, String> namespacesMap) { Stack<Element> elementPath = new Stack<Element>(); // since we need the mapping the other way round, we invert the map Map<String, String> namespaceUriToIdMap = new HashMap<String, String>(); for (Entry<String, String> entry : namespacesMap.entrySet()) { namespaceUriToIdMap.put(entry.getValue(), entry.getKey()); } // recursively find all ancestors of targetElement (up to, not including, rootElement) { Element currentElement = targetElement; while (currentElement != null && currentElement != rootElement) { elementPath.push(currentElement); Node parent = currentElement.getParentNode(); if (parent instanceof Element) { currentElement = (Element) currentElement.getParentNode(); } else { currentElement = null; } } } // construct XPath StringBuilder builder = new StringBuilder(); while (!elementPath.isEmpty()) { Element currentElement = elementPath.pop(); if (builder.length() > 0) { // don't include "/" at the beginning builder.append("/"); } if (namespacesMap != null) { String namespace = currentElement.getNamespaceURI(); if (namespace != null) { namespace = namespaceUriToIdMap.get(namespace); builder.append(namespace); builder.append(":"); } } builder.append(currentElement.getLocalName()); if (includeElementIndex) { int index = getElementIndex(currentElement); builder.append("["); builder.append(index); builder.append("]"); } } return builder.toString(); } /** * Returns the index of element in the list of all elements with the same name in its parent node. * If element's parent node is null, this function returns 0. */ public static int getElementIndex(Element element) { int index = 1; Node sibling = element; while ((sibling = sibling.getPreviousSibling()) != null) { if (sibling instanceof Element) { Element siblingElement = (Element) sibling; // check if element names and element namespaces match if (element.getLocalName().equals(siblingElement.getLocalName()) && (element.getNamespaceURI() == null ? siblingElement.getNamespaceURI() == null : element.getNamespaceURI().equals(siblingElement.getNamespaceURI()))) { ++index; } } } return index; } }