/** * Returns a given node at the relative path. * * @param xpath the xpath used to retrieve the resulting node * @return the node returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nullable public StructuredNode queryNode(String xpath) { try { Node result = (Node) compile(xpath).evaluate(node, XPathConstants.NODE); if (result == null) { return null; } return new StructuredNode(result); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
/** * Returns the property at the given relative path as string. * * @param path the xpath used to retrieve property * @return a string representation of the value returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nullable public String queryString(String path) { try { Object result = compile(path).evaluate(node, XPathConstants.NODE); if (result == null) { return null; } if (result instanceof Node) { String s = ((Node) result).getTextContent(); if (s != null) { return s.trim(); } return s; } return result.toString().trim(); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
/** * Returns a list of nodes at the relative path. * * @param xpath the xpath used to retrieve the resulting nodes * @return the list of nodes returned by the given xpath expression * @throws IllegalArgumentException if an invalid xpath was given */ @Nonnull public List<StructuredNode> queryNodeList(String xpath) { try { NodeList result = (NodeList) compile(xpath).evaluate(node, XPathConstants.NODESET); List<StructuredNode> resultList = new ArrayList<>(result.getLength()); for (int i = 0; i < result.getLength(); i++) { resultList.add(new StructuredNode(result.item(i))); } return resultList; } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }