Java XPath Expression valueOf(Node node, String xpath, NamespaceContext context)

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

Description

Evaluates an XPath expression given a namespace context and returns the String value of the result, similar to the value-of function in XSLT.

License

Open Source License

Parameter

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

Return

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

Declaration

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

Method Source Code


//package com.java2s;
/*/* w  w w.jav 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 {@link String} value
     * of the result, similar to the <code>value-of</code> function in XSLT.
     * If there is an error in the XPath syntax, an empty string will be
     * returned.
     * @param node the {@link Node} to evaluate the expression on
     * @param xpath the XPath expression to evaluate
     * @return a {@link String}.
     */
    public static String valueOf(Node node, String xpath) {
        try {
            return (String) xpathObj.evaluate(xpath, node, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            return "";
        }
    }

    /**
     * Evaluates an XPath expression given a namespace context and returns
     * the {@link String} value of the result, similar to the
     * <code>value-of</code> function in XSLT. If there is an error in the
     * XPath syntax, an empty string will be returned. 
     * @param node the {@link Node} to evaluate the expression on
     * @param xpath the XPath expression to evaluate
     * @return a {@link NodeList}, or <code>null</code> if there is an
     * error in the XPath expression.
     */
    public static String valueOf(Node node, String xpath, NamespaceContext context) {
        try {
            xpathObj.setNamespaceContext(context);
            String result = (String) xpathObj.evaluate(xpath, node, XPathConstants.STRING);
            xpathObj.reset();
            return result;
        } catch (XPathExpressionException e) {
            return "";
        }
    }
}

Related

  1. replaceXPathNode(Node replaceThis, Node replaceWith)
  2. singleXPathNode(String expression, Node node)
  3. streamNodes(String xpath, Object node)
  4. string(Node context, String expression)
  5. string(String fileName, String xpathExpression)
  6. xmlValueOf(Node node, String xpathExpression)
  7. xPathStr(String expr, Object context)
  8. xpathToNode(String xpathQuery, Object domObject)
  9. XPathValueFromString(String sIn, String sxpath)