Java XPath Get getNodeByXPath(Document document, String xPath)

Here you can find the source of getNodeByXPath(Document document, String xPath)

Description

Inverse function for #getXPath(Node) .

License

Open Source License

Parameter

Parameter Description
document ancestor document for xPath
xPath XPath to a node in one of the following form: <code> <br>&nbsp;"/html/body/table/tr/td", <br>&nbsp;"/html/body/table/tr/td[1]", <br>&nbsp;"/html/body/table/tr/td[1]/@onclick".</code>

Return

node for the given xPath , or null if the node is not found.

Declaration

public static Node getNodeByXPath(Document document, String xPath) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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://from   w ww .  j  a v  a2s.c o  m
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    /**
     * Inverse function for {@link #getXPath(Node)}.
     * 
     * @param document ancestor document for xPath
     * @param xPath XPath to a node in one of the following form:
     * <code>
     * <br>&nbsp;"/html/body/table/tr/td",
     * <br>&nbsp;"/html/body/table/tr/td[1]",
     * <br>&nbsp;"/html/body/table/tr/td[1]/@onclick".</code>
     * 
     * @return node for the given {@code xPath}, or {@code null}
     * if the node is not found.
     */
    public static Node getNodeByXPath(Document document, String xPath) {
        Node currentNode = document;
        String[] nodeNames = xPath.split("/"); //$NON-NLS-1$

        // begin from 1 to skip the first element which is empty
        for (int i = 1; i < nodeNames.length; i++) {
            String nodeName = nodeNames[i];
            if (nodeName.charAt(0) != '@') {
                currentNode = currentNode.getFirstChild();
                if (nodeName.charAt(nodeName.length() - 1) != ']') {
                    while (currentNode.getNodeType() != Node.ELEMENT_NODE
                            || !currentNode.getNodeName().equals(nodeName)) {
                        currentNode = currentNode.getNextSibling();
                    }
                } else {
                    int openingBracketIndex = nodeName.lastIndexOf('[');
                    String stringPosition = nodeName.substring(openingBracketIndex + 1, nodeName.length() - 1);
                    nodeName = nodeName.substring(0, openingBracketIndex);

                    int position = Integer.parseInt(stringPosition);
                    int curPosition = 0;
                    while (true) {
                        if (currentNode.getNodeType() == Node.ELEMENT_NODE
                                && currentNode.getNodeName().equals(nodeName)) {
                            ++curPosition;
                            if (curPosition == position) {
                                break;
                            }
                        }
                        currentNode = currentNode.getNextSibling();
                    }
                }
            } else {
                String attributeName = nodeName.substring(1, nodeName.length());
                currentNode = currentNode.getAttributes().getNamedItem(attributeName);
            }
        }

        return currentNode;
    }
}

Related

  1. getNameBasedXPath(Node n, boolean includeCurrent)
  2. getNewXPath()
  3. getNewXPath()
  4. getNode(Node node, XPathExpression xpath)
  5. getNode(String xPathExpression, Node node)
  6. getNodeByXPath(Object context, String expression)
  7. getNodeList(Element root, String xpath)
  8. getNodeList(File xml, String xpathExpression)
  9. getNodeList(final String expr, final File xmlFile)