Java XPath Create getXPathListForNode(Node n)

Here you can find the source of getXPathListForNode(Node n)

Description

Returns a list of tag names representing the path from the document root to the given node n.

License

Apache License

Parameter

Parameter Description
n the node for which retrieve the path.

Return

a sequence of HTML tag names.

Declaration

public static String[] getXPathListForNode(Node n) 

Method Source Code

//package com.java2s;
/*/*from w w w.j  av a 2  s.co m*/
 * Copyright 2008-2010 Digital Enterprise Research Institute (DERI)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * Returns a list of tag names representing the path from
     * the document root to the given node <i>n</i>.
     *
     * @param n the node for which retrieve the path.
     * @return a sequence of HTML tag names.
     */
    public static String[] getXPathListForNode(Node n) {
        if (n == null) {
            return EMPTY_STRING_ARRAY;
        }
        List<String> ancestors = new ArrayList<String>();
        ancestors.add(String.format("%s[%s]", n.getNodeName(), getIndexInParent(n)));
        Node parent = n.getParentNode();
        while (parent != null) {
            ancestors.add(0, String.format("%s[%s]", parent.getNodeName(), getIndexInParent(parent)));
            parent = parent.getParentNode();
        }
        return ancestors.toArray(new String[ancestors.size()]);
    }

    /**
     * Given a node this method returns the index corresponding to such node
     * within the list of the children of its parent node.
     *
     * @param n the node of which returning the index.
     * @return a non negative number.
     */
    public static int getIndexInParent(Node n) {
        Node parent = n.getParentNode();
        if (parent == null) {
            return 0;
        }
        NodeList nodes = parent.getChildNodes();
        int counter = -1;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node current = nodes.item(i);
            if (current.getNodeType() == n.getNodeType() && current.getNodeName().equals(n.getNodeName())) {
                counter++;
            }
            if (current.equals(n)) {
                return counter;
            }
        }
        throw new IllegalStateException("Cannot find a child within its parent node list.");
    }
}

Related

  1. getXPathForNode(Node node)
  2. getXPathForNode(Node node)
  3. getXPathFromVector(Vector path)
  4. getXPathFromVector(Vector path)
  5. getXPathFromVector(Vector path)
  6. getXPathNoCache(String exp)
  7. getXPathNodeIndex(Node node, boolean ignoreWhitespace)
  8. getXPathNodes(XPath xpath, Object inic, String name)
  9. getXPathToContent(final Node root, final String selectedContent)