Java XPath Evaluate evaluateAsString(String expression, Node node)

Here you can find the source of evaluateAsString(String expression, Node node)

Description

Evaluates the specified expression on the specified node and returns the result as a String.

License

Open Source License

Parameter

Parameter Description
expression The Xpath expression to evaluate.
node The node on which to evaluate the expression.

Exception

Parameter Description
XPathExpressionException If there are any problems evaluating theXpath expression.

Return

The result of evaluating the specified expression, or null if the evaluation didn't return any result.

Declaration

private static String evaluateAsString(String expression, Node node) throws XPathExpressionException 

Method Source Code

//package com.java2s;
/*//w w w .ja v a 2 s .  c  o m
 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import org.w3c.dom.Node;

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

public class Main {
    /**
     * Evaluates the specified expression on the specified node and returns the
     * result as a String.
     *
     * @param expression The Xpath expression to evaluate.
     * @param node The node on which to evaluate the expression.
     * @return The result of evaluating the specified expression, or null if the
     *         evaluation didn't return any result.
     * @throws XPathExpressionException If there are any problems evaluating the
     *             Xpath expression.
     */
    private static String evaluateAsString(String expression, Node node) throws XPathExpressionException {
        if (isEmpty(node))
            return null;

        if (!expression.equals(".")) {
            /*
             * If the expression being evaluated doesn't select a node, we want
             * to return null to distinguish between cases where a node isn't
             * present (which should be represented as null) and when a node is
             * present, but empty (which should be represented as the empty
             * string). We skip this test if the expression is "." since we've
             * already checked that the node exists.
             */
            if (asNode(expression, node) == null)
                return null;
        }

        String s = xpath().evaluate(expression, node);

        return s.trim();
    }

    /**
     * Returns true if the specified node is null or has no children.
     *
     * @param node The node to test.
     * @return True if the specified node is null or has no children.
     */
    public static boolean isEmpty(Node node) {
        return (node == null);
    }

    public static Node asNode(String nodeName, Node node) throws XPathExpressionException {
        if (node == null)
            return null;
        return (Node) xpath().evaluate(nodeName, node, XPathConstants.NODE);
    }

    /**
     * Returns a new instance of XPath, which is not thread safe and not
     * reentrant.
     */
    public static XPath xpath() {
        return XPathFactory.newInstance().newXPath();
    }
}

Related

  1. evaluate(File xmlFile, String xPathExpression)
  2. evaluate(Node node, XPathExpression expression)
  3. evaluate(Object obj, String xpathExpression, QName qName)
  4. evaluate(String path, Node node)
  5. evaluate(XPath xpath, String base, String path, Document document)
  6. evaluateAsString(String expression, Node node)
  7. evaluateNodeListTextXPath(final Document document, final String expression)
  8. evaluateNodeListXPath(final Document document, final String expression)
  9. evaluateNodes(final XPathExpression xpathExpression, final Document pom)