List of utility methods to do XPath Select
NodeList | selectNodes(String express, Object source) select Nodes NodeList result = null; XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); return result; |
NodeList | selectNodes(String xPath, Node target) select Nodes XPath xpathForUri = XPathFactory.newInstance().newXPath(); NodeList nodes = null; try { XPathExpression expr = xpathForUri.compile(xPath); nodes = (NodeList) expr.evaluate(target, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new RuntimeException(e); return nodes; |
NodeList | selectNodes(String xpath, Object node) Select all children node of a xpath try { return (NodeList) getXPath(xpath).evaluate(node, XPathConstants.NODESET); } catch (Exception e) { throw new RuntimeException(e); |
Node[] | selectNodes(String xpath, Object node) select Nodes return streamNodes(xpath, node).toArray(Node[]::new); |
List | selectNodesViaXPath(XPath xPath, Node startingNode, String xPathExpression) select Nodes Via X Path List<Node> data = new ArrayList<Node>(); XPathExpression expression = xPath.compile(xPathExpression); Object result = expression.evaluate(startingNode, XPathConstants.NODESET); NodeList nodeList = (NodeList) result; for (int index = 0; index < nodeList.getLength(); index++) { data.add(nodeList.item(index)); return data; ... |
String | selectNodeText(Node node, String expression) select Node Text XPathFactory factory = XPathFactory.newInstance();
XPath path = factory.newXPath();
Object result = path.evaluate(expression, node, XPathConstants.NODE);
return getNodeText((Node) result);
|
Element | selectSingleElement(Element element, String xpathExpression) select Single Element if (xpathExpression.indexOf("/") == -1) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) { return (Element) node; return null; } else { XPath xpath = XPathFactory.newInstance().newXPath(); Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE); return node; |
Node | selectSingleNode(final Node node, final String xPath) select Single Node return (Node) _xpf.newXPath().evaluate(xPath, node, XPathConstants.NODE);
|
Node | selectSingleNode(final Node sourceNode, final String xPathExpression) select Single Node Node result; XPathFactory factory = XPathFactory.newInstance(); XPath xPathParser = factory.newXPath(); try { result = (Node) xPathParser.evaluate(xPathExpression, sourceNode, XPathConstants.NODE); } catch (XPathExpressionException e) { result = null; return result; |
Node | selectSingleNode(Node node, String xpath, NamespaceContext context) Evaluates an XPath expression given a namespace context, and returns the result as a Node . try { xpathObj.setNamespaceContext(context); Node result = (Node) xpathObj.evaluate(xpath, node, XPathConstants.NODE); xpathObj.reset(); return result; } catch (XPathExpressionException e) { return null; |