/** * Wraps the given W3C node into a structured node. * * @param node the node to wrap * @return a wrapped instance of the given node */ @Nonnull public static StructuredNode of(@Nonnull Node node) { return new StructuredNode(node); }
/** * Returns a list of all children of this DOM node. * * @return a list containing all children of this node. If no children exist, an empty list will be returned. */ @Nonnull public List<StructuredNode> getChildren() { NodeList result = node.getChildNodes(); List<StructuredNode> resultList = new ArrayList<>(result.getLength()); for (int i = 0; i < result.getLength(); i++) { resultList.add(new StructuredNode(result.item(i))); } return resultList; }
/** * 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); } }
/** * 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); } }