Java XPath Create getXPath(Node node)

Here you can find the source of getXPath(Node node)

Description

get X Path

License

Open Source License

Declaration

public static String getXPath(Node node) 

Method Source Code

//package com.java2s;
/*//from ww w.j a v  a  2  s . com
 *
 * 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.util.Stack;

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

public class Main {
    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();
    }
}

Related

  1. getXPath(NamespaceContext nsContext)
  2. getXPath(Node n)
  3. getXPath(Node n)
  4. getXPath(Node n)
  5. getXPath(Node n)
  6. getXPath(Node node)
  7. getXPath(Node node)
  8. getXPath(Node node, String xpath)
  9. getXPath(String exp, NamespaceContext ctx)