Java XPath Evaluate asByte(String expression, Node node)

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

Description

Evaluates the specified XPath expression and returns the result as a Byte.

License

Open Source License

Parameter

Parameter Description
expression The XPath expression to evaluate.
node The node to run the expression on.

Exception

Parameter Description
XPathExpressionException If there was a problem processing thespecified XPath expression.

Return

The Byte result.

Declaration

public static Byte asByte(String expression, Node node) throws XPathExpressionException 

Method Source Code

//package com.java2s;
/*/*from w  w w.j a va2s .com*/
 * 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 XPath expression and returns the result as a
     * Byte.
     *
     * @param expression The XPath expression to evaluate.
     * @param node The node to run the expression on.
     * @return The Byte result.
     * @throws XPathExpressionException If there was a problem processing the
     *             specified XPath expression.
     */
    public static Byte asByte(String expression, Node node) throws XPathExpressionException {
        String byteString = evaluateAsString(expression, node);
        return (isEmptyString(byteString)) ? null : Byte.valueOf(byteString);
    }

    /**
     * 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 string is null or empty.
     *
     * @param s The string to test.
     * @return True if the specified string is null or empty.
     */
    private static boolean isEmptyString(String s) {
        if (s == null)
            return true;
        if (s.trim().equals(""))
            return true;

        return false;
    }

    /**
     * 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. asByte(String expression, Node node)
  2. convertToXpath(String qname)
  3. evaluate(Document doc)
  4. evaluate(File xmlFile, String xPathExpression)
  5. evaluate(Node node, XPathExpression expression)