Java tutorial
//package com.java2s; /* * * This 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 software 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. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.io.PrintStream; import java.util.Stack; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { static void dumpXpath(Node node, PrintStream printer) { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node item = list.item(i); printer.println("Xpath: " + getXPath(item)); printer.println("Text Content: " + item.getTextContent()); if (item.hasChildNodes()) { dumpXpath(item, printer); } } } public static String getXPath(Node node) { if (null == node) return null; // declarations Node parent = null; Stack<Node> hierarchy = new Stack<Node>(); StringBuilder buffer = new StringBuilder(); // push element on stack hierarchy.push(node); parent = node.getParentNode(); while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) { // push on stack hierarchy.push(parent); // get parent of parent parent = parent.getParentNode(); } // construct xpath Object obj = null; while (!hierarchy.isEmpty() && null != (obj = hierarchy.pop())) { Node n = (Node) obj; boolean handled = false; // only consider elements if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; // is this the root element? if (buffer.length() == 0) { // root element - simply append element name buffer.append(n.getNodeName()); } else { // child element - append slash and element name buffer.append("/"); buffer.append(n.getNodeName()); if (n.hasAttributes()) { // see if the element has a name or id attribute if (e.hasAttribute("id")) { // id attribute found - use that buffer.append("[@id='" + e.getAttribute("id") + "']"); handled = true; } else if (e.hasAttribute("name")) { // name attribute found - use that buffer.append("[@name='" + e.getAttribute("name") + "']"); handled = true; } } if (!handled) { // no known attribute we could use - get sibling index int prev_siblings = 1; Node prev_sibling = n.getPreviousSibling(); while (null != prev_sibling) { if (prev_sibling.getNodeType() == n.getNodeType()) { if (prev_sibling.getNodeName().equalsIgnoreCase(n.getNodeName())) { prev_siblings++; } } prev_sibling = prev_sibling.getPreviousSibling(); } buffer.append("[" + prev_siblings + "]"); } } } } // return buffer return buffer.toString(); } }