Java XPath Select selectSingleNode(Node node, String xpath, NamespaceContext context)

Here you can find the source of selectSingleNode(Node node, String xpath, NamespaceContext context)

Description

Evaluates an XPath expression given a namespace context, and returns the result as a Node .

License

Open Source License

Parameter

Parameter Description
node the Node to evaluate the expression on
xpath the XPath expression to evaluate
context the namespace context

Return

a single , or null if there is an error in the XPath expression.

Declaration

public static Node selectSingleNode(Node node, String xpath, NamespaceContext context) 

Method Source Code


//package com.java2s;
/*//  w  w  w. j a  v  a 2 s .  co m
 * org.daisy.util (C) 2005-2008 Daisy Consortium
 * 
 * This library 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 library 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 library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;

public class Main {
    private static XPath xpathObj = XPathFactory.newInstance().newXPath();

    /**
     * Evaluates an XPath expression and returns the result as a {@link Node}.
     * Only a single <code>Node</code> will be returned.
     * @param node the {@link Node} to evaluate the expression on
     * @param xpath the XPath expression to evaluate
     * @return a single {@link Node}, or <code>null</code> if there is an
     * error in the XPath expression.
     */
    public static Node selectSingleNode(Node node, String xpath) {
        try {
            return (Node) xpathObj.evaluate(xpath, node, XPathConstants.NODE);
        } catch (XPathExpressionException e) {
            return null;
        }
    }

    /**
     * Evaluates an XPath expression given a namespace context, and returns the result as a {@link Node}.
     * Only a single <code>Node</code> will be returned.
     * @param node the {@link Node} to evaluate the expression on
     * @param xpath the XPath expression to evaluate
     * @param context the namespace context
     * @return a single {@link Node}, or <code>null</code> if there is an
     * error in the XPath expression.
     */
    public static Node selectSingleNode(Node node, String xpath, NamespaceContext context) {
        try {
            xpathObj.setNamespaceContext(context);
            Node result = (Node) xpathObj.evaluate(xpath, node, XPathConstants.NODE);
            xpathObj.reset();
            return result;
        } catch (XPathExpressionException e) {
            return null;
        }
    }
}

Related

  1. selectNodesViaXPath(XPath xPath, Node startingNode, String xPathExpression)
  2. selectNodeText(Node node, String expression)
  3. selectSingleElement(Element element, String xpathExpression)
  4. selectSingleNode(final Node node, final String xPath)
  5. selectSingleNode(final Node sourceNode, final String xPathExpression)
  6. selectString(String xpath, Object node)
  7. selectStrings(String xpath, Object node)
  8. selectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)
  9. xmlSelectNodes(Node node, String xpathExpression)