Java XPath Select getValueFromXPath(Document document, String xpathString)

Here you can find the source of getValueFromXPath(Document document, String xpathString)

Description

Returns the string value of evaluated xpath

License

Open Source License

Parameter

Parameter Description
document - XML Document to check
xpathString - xpath to evaluate

Return

String evaluation of the xpath

Declaration

public static String getValueFromXPath(Document document, String xpathString) 

Method Source Code

//package com.java2s;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

public class Main {
    /***//from  w  w  w.  ja v a  2s . c  om
     * Returns the string value of evaluated xpath
     *
     * @param  document    - XML Document to check
     * @param  xpathString - xpath to evaluate
     * @return String evaluation of the xpath
     */
    public static String getValueFromXPath(Document document, String xpathString) {
        try {
            return (String) XPathFactory.newInstance().newXPath().evaluate(xpathString, document,
                    XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            throw new RuntimeException("Error evaluating xpath [" + xpathString + "] -- " + e.getMessage());
        }
    }
}

Related

  1. getValueByXPath(Document document, String xpath)
  2. getValueByXPathAsInt(Document document, String xpath)
  3. getXmlElements(Document inXml, String xpath)
  4. selectElements(Element element, String xpathExpression)
  5. selectNodeIterator(Node nContextNode, String sXPath)
  6. selectNodeList(Node contextNode, String expression)