Java XPath Expression extractNode(XPath xpath, String nodePath, String xmlString)

Here you can find the source of extractNode(XPath xpath, String nodePath, String xmlString)

Description

Extract a String node from an XML Message

License

Open Source License

Parameter

Parameter Description
xpath XPath object
nodePath XPath statement to locate the node
xmlString Xml string object to extract the data from

Return

The requested data, or "" if not found.

Declaration

public static String extractNode(XPath xpath, String nodePath, String xmlString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.xml.sax.InputSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import java.io.StringReader;

public class Main {
    /**//from   w  w w  .  j av  a2  s .  c  o m
     * Extract a String node from an XML Message
     *
     * @param xpath     XPath object
     * @param nodePath  XPath statement to locate the node
     * @param xmlString Xml string object to extract the data from
     * @return The requested data, or "" if not found.
     */
    public static String extractNode(XPath xpath, String nodePath, String xmlString) {
        InputSource inputSource = new InputSource(new StringReader(xmlString));

        try {
            return (String) xpath.evaluate(nodePath, inputSource, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            return "";
        }
    }
}

Related

  1. createXPathExpression(String xpathString)
  2. deleteXMLElement(String xml, String xpath)
  3. dumpXpath(Node node, PrintStream printer)
  4. executePath(final Node node, final XPathExpression expression)
  5. expr(String xpathStr)
  6. extractNodeList(XPath xpath, String nodePath, String xmlString)
  7. extractNodes(File xmlFile, String xpathLocator)
  8. extractValue(String xml, String xpathExpression)
  9. findElements(final String xPathExpression, final Element root)