Java XPath Get getValueByXpath(Node doc, String xquery)

Here you can find the source of getValueByXpath(Node doc, String xquery)

Description

get Value By Xpath

License

Apache License

Declaration

public static String getValueByXpath(Node doc, String xquery) 

Method Source Code

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

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;

public class Main {
    public static String getValueByXpath(Node doc, String xquery) {
        try {//w w  w.j  ava 2  s . com
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expression = xpath.compile(xquery);
            String result = (String) expression.evaluate(doc, XPathConstants.STRING);
            return result;
        } catch (Exception e) {

        }
        return null;
    }
}

Related

  1. getText(String xPathExpression, Node node)
  2. getTextNodes(Node contextNode, String xPath)
  3. getValidXpath(String xPath, NamespaceContext context)
  4. getValue(final String expression, final String xml)
  5. getValueByPath(Node node, String path)
  6. getValueFromXML(final String inputXML, final String xPathQuery, final int index)
  7. getValues(final String expression, final String xml)
  8. getX_PATH()
  9. xGetNode(Node targetNode, String expression)