Java XPath Get getString(final String xPath, final Object item)

Here you can find the source of getString(final String xPath, final Object item)

Description

Evaluates the xpath on an object

License

Apache License

Parameter

Parameter Description
xPath the xpath expression to use
item the item to perform the xpath on

Exception

Parameter Description
XPathExpressionException represents an error in an XPath expression

Return

the evaluated object

Declaration

public static String getString(final String xPath, final Object item) throws XPathExpressionException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    /**// w  w  w.  j a v  a 2s .com
     * Evaluates the xpath on an object
     * 
     * @param xPath
     * the xpath expression to use
     * @param item
     * the item to perform the xpath on
     * @return the evaluated object
     * @throws XPathExpressionException
     * represents an error in an XPath expression
     */
    public static String getString(final String xPath, final Object item) throws XPathExpressionException {
        return (String) evaluateXPath(xPath, item, XPathConstants.STRING);
    }

    /**
     * Evaluates the xpath on an object
     * 
     * @param xPath
     * the xpath expression to use
     * @param item
     * the item to perform the xpath on
     * @param returnType
     * the expected returntype of the evaluated expression
     * @return the evaluated object
     * @throws XPathExpressionException
     * represents an error in an XPath expression
     */
    public static Object evaluateXPath(final String xPath, final Object item, final QName returnType)
            throws XPathExpressionException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(xPath);
        return expr.evaluate(item, returnType);
    }
}

Related

  1. getSearchHandlerNode(final File solrconfig)
  2. getSharedXPath()
  3. getSpeficValueFromNode(Node n, String xpathExpr)
  4. getStrFromNode(Node xpathnode)
  5. getStrFromNode(Node xpathnode)
  6. getString(Node node, XPathExpression expr)
  7. getStringByXPath(String xml, String xpathStr)
  8. getStringValue(Node node, XPathExpression expression)
  9. getStringValue(Object doc, XPathExpression path)