List of usage examples for javax.xml.xpath XPathExpression evaluate
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;
From source file:Main.java
public static NodeList getNogeList(String path, Node node) throws RuntimeException { path = path.trim();//from ww w . j a v a2 s . co m try { XPath xp = XPathFactory.newInstance().newXPath(); XPathExpression expr = xp.compile(path); NodeList lst = (NodeList) expr.evaluate(node, XPathConstants.NODESET); return lst; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
public static ArrayList<Node> getNodesXPath(String srcXmlString, String xPath) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // never forget this! Document doc = null;/*from w ww. ja va2 s.c om*/ DocumentBuilder builder = null; ArrayList<Node> nodesList = new ArrayList<Node>(); try { builder = domFactory.newDocumentBuilder(); doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(xPath); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList xPathNodes = (NodeList) result; logger.debug("xpath result count: " + xPathNodes.getLength()); // iterate through all the nodes for (int i = 0; i < xPathNodes.getLength(); i++) { nodesList.add(xPathNodes.item(i)); } } catch (Exception ex) { logger.error(ex.getMessage()); } return nodesList; }
From source file:Main.java
/** * <p>/*from w w w . j a va2s. c o m*/ * Evaluates the given XPath expression in the context of the given item, with * the expectation of a node result. * </p> * * @param xpe * An XPath expression. * @param item * A context item. * @return The result of the evaluation as a {@link Node} instance. * @throws XPathExpressionException * if the evaluation fails * @since 1.67.5 * @see XPathConstants#NODE */ public static Node evaluateNode(XPathExpression xpe, Object item) throws XPathExpressionException { return (Node) xpe.evaluate(item, XPathConstants.NODE); }
From source file:Main.java
private static List<String> getEmployeeNameWithAge(Document doc, XPath xpath, int age) throws Exception { List<String> list = new ArrayList<>(); XPathExpression expr = xpath.compile("/Employees/Employee[age>" + age + "]/name/text()"); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); return list;// w w w . j a v a2s .c om }
From source file:Main.java
/** * <p>/* ww w . j a v a 2 s .c om*/ * Evaluates the given XPath expression in the context of the given item, with * the expectation of a string result. * </p> * * @param xpe * An XPath expression. * @param item * A context item. * @return The result of the evaluation as a {@link String}. * @throws XPathExpressionException * if the evaluation fails * @since 1.67.5 * @see XPathConstants#STRING */ public static String evaluateString(XPathExpression xpe, Object item) throws XPathExpressionException { return (String) xpe.evaluate(item, XPathConstants.STRING); }
From source file:Main.java
/** * <p>//from w w w . j av a 2 s . c om * Evaluates the given XPath expression in the context of the given item, with * the expectation of a number (Java <code>double</code>) result. * </p> * * @param xpe * An XPath expression. * @param item * A context item. * @return The result of the evaluation as a {@link Double} instance. * @throws XPathExpressionException * if the evaluation fails * @since 1.67.5 * @see XPathConstants#NUMBER */ public static Double evaluateNumber(XPathExpression xpe, Object item) throws XPathExpressionException { return (Double) xpe.evaluate(item, XPathConstants.NUMBER); }
From source file:Main.java
/** * Evaluate xpath./* w ww. j a v a2s . c o m*/ * * @param doc * the doc * @param expr * the expr * @return the object */ public static NodeList evaluateXpath(Document doc, XPathExpression expr) { NodeList XmlResult = null; try { XmlResult = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return XmlResult; }
From source file:Main.java
/** * <p>// ww w .j ava 2 s .c o m * Evaluates the given XPath expression in the context of the given item, with * the expectation of a boolean result. * </p> * * @param xpe * An XPath expression. * @param item * A context item. * @return The result of the evaluation as a {@link Boolean} instance. * @throws XPathExpressionException * if the evaluation fails * @since 1.67.5 * @see XPathConstants#BOOLEAN */ public static Boolean evaluateBoolean(XPathExpression xpe, Object item) throws XPathExpressionException { return (Boolean) xpe.evaluate(item, XPathConstants.BOOLEAN); }
From source file:Main.java
@SuppressWarnings("unchecked") private static <T> T evaluateXpath(String expression, Document document, QName dataType) { try {//from ww w . j a va 2 s . c o m XPath xpath = createXPath(); XPathExpression compiledExpression = xpath.compile(expression); return (T) compiledExpression.evaluate(document, dataType); } catch (XPathExpressionException e) { throw new IllegalArgumentException("Cannot evaluate XPath expression '" + expression + "'"); } }
From source file:Main.java
public static NodeList queryNodeList(Document document, String xPath) { try {// w ww .jav a2s. c om XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath); return (NodeList) expr.evaluate(document, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }