Java XPath Expression string(Node context, String expression)

Here you can find the source of string(Node context, String expression)

Description

string

License

Apache License

Declaration

static public String string(Node context, String expression) 

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.XPathExpressionException;

import org.w3c.dom.Node;

public class Main {
    static private XPath xpath;

    static public String string(Node context, String expression) {
        try {//from w ww  . j  av  a 2 s .c o m
            String result = (String) xpath.evaluate(expression, context, XPathConstants.STRING);
            if (result == null || result.length() == 0)
                return null;
            else
                return result;
        } catch (XPathExpressionException ex) {
            ex.printStackTrace();
            throw new RuntimeException("invalid xpath expresion used");
        }
    }

    static public String string(Node context, String expression, String defaultValue) {
        try {
            String result = (String) xpath.evaluate(expression, context, XPathConstants.STRING);
            if (result == null || result.length() == 0)
                return defaultValue;
            else
                return result;
        } catch (XPathExpressionException ex) {
            ex.printStackTrace();
            throw new RuntimeException("invalid xpath expresion used");
        }
    }
}

Related

  1. pathExists(XPath xpath, String expression, Object object)
  2. readXmlNodeChildFromFile(String xPath, String path, NamespaceContext nsContext)
  3. replaceXPathNode(Node replaceThis, Node replaceWith)
  4. singleXPathNode(String expression, Node node)
  5. streamNodes(String xpath, Object node)
  6. string(String fileName, String xpathExpression)
  7. valueOf(Node node, String xpath, NamespaceContext context)
  8. xmlValueOf(Node node, String xpathExpression)
  9. xPathStr(String expr, Object context)