/** * Evaluates the specified XPath expression and returns the result as a * Node. * * @param nodeName * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Node result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Node asNode(String nodeName, Node node) throws XPathExpressionException { return asNode(nodeName, node, xpath()); }
if (asNode(expression, node, xpath) == null) return null;
/** * Evaluates the specified XPath expression and returns the result as a * Node. * * @param nodeName * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Node result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Node asNode(String nodeName, Node node) throws XPathExpressionException { return asNode(nodeName, node, xpath()); }
if (asNode(expression, node, xpath) == null) return null;
/** * 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 (!".".equals(expression)) { /* * 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(); }
@Test public void testAsNode() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node node = XpathUtils.asNode("Foo/Title", document); assertNotNull(node); assertEquals("Title", node.getNodeName()); }
@Test public void testIsEmpty() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node emptyNode = XpathUtils.asNode("Foo/Fake", document); Node realNode = XpathUtils.asNode("Foo/Count", document); assertTrue(XpathUtils.isEmpty(emptyNode)); assertFalse(XpathUtils.isEmpty(realNode)); }
/** * Evaluates the specified XPath expression and returns the result as a * Node. * * @param nodeName * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Node result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Node asNode(String nodeName, Node node) throws XPathExpressionException { return asNode(nodeName, node, xpath()); }
if (asNode(expression, node, xpath) == null) return null;
/** * 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 (!".".equals(expression)) { /* * 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(); }
/** * 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 (!".".equals(expression)) { /* * 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(); }