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 getXpathExpressionNodeList(Object xprContext, String xpExpression) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression xpe = xpath.compile(xpExpression); NodeList nodes = (NodeList) xpe.evaluate(xprContext, XPathConstants.NODESET); return nodes; }
From source file:Main.java
public static String getValueByXpath(Node doc, String xquery) { try {/*from w ww .j a v a 2 s . c o m*/ XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expression = xpath.compile(xquery); String result = (String) expression.evaluate(doc, XPathConstants.STRING); return result; } catch (Exception e) { } return null; }
From source file:Main.java
public static NodeList evaluateXPathExpr(XPathExpression xpath, Node node) throws XPathExpressionException { return (NodeList) xpath.evaluate(node, XPathConstants.NODESET); }
From source file:Main.java
public static String getValueAsString(String filePath, String value) throws ParserConfigurationException, SAXException, IOException { String id = null;//from ww w. j av a2 s .c o m XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { XPathExpression expr = xpath.compile(value); id = (String) expr.evaluate(getDoc(filePath), XPathConstants.STRING); } catch (XPathExpressionException e) { e.printStackTrace(); } return id; }
From source file:Main.java
/** * convenience method to evaluate an xpath expression against a DOM document * @param DOM the document//from w w w. ja v a2s . co m * @param xpath the xpath * @return NodeList of retrieved nodes * @throws XPathExpressionException */ public static NodeList evaluateXPath(Document DOM, XPathExpression xpath) throws XPathExpressionException { NodeList result = (NodeList) xpath.evaluate(DOM, XPathConstants.NODESET); return result; }
From source file:Main.java
public static String[] xpathOnString(String q, String stringdoc) { try {//w ww .j a v a 2s . c o m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(stringdoc.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(q); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String res[] = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { // System.out.println(nodes.item(i).toString()); res[i] = nodes.item(i).getNodeValue(); } return res; } catch (Exception e) { System.out.println("XPathUtils.xpathOnString:caught:" + e); return null; } }
From source file:Main.java
public static String orEmptyStr(XPathExpression xpe, Node n) throws XPathExpressionException { Node inner = (Node) xpe.evaluate(n, XPathConstants.NODE); return inner == null ? "" : inner.getTextContent(); }
From source file:Main.java
/** * Uses the passed XPath expression to locate a single node in the passed element. * @param targetNode The node to search. * @param expression The XPath expression. * @return The located node or null if one is not found. *//*ww w . j a va 2 s. c om*/ public static Node xGetNode(Node targetNode, String expression) { Node node = null; XPath xpath = null; try { xpath = XPathFactory.newInstance().newXPath(); XPathExpression xpathExpression = xpath.compile(expression); node = (Node) xpathExpression.evaluate(targetNode, NODE); return node; } catch (Exception e) { throw new RuntimeException("XPath:Failed to locate the node:" + expression, e); } }
From source file:Main.java
public static Element SelectSingleElement(Element parent, String expression) { Element element = null;/*from w ww. j a v a2 s. com*/ XPath xpath = getXPath(); try { XPathExpression xpathExpression = xpath.compile(expression); element = (Element) xpathExpression.evaluate(parent, XPathConstants.NODE); } catch (Exception e) { throw new RuntimeException(e); } return element; }
From source file:Main.java
public static final List<Node> selectNodesViaXPath(XPath xPath, Node startingNode, String xPathExpression) throws XPathExpressionException { 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));/*w ww .j a v a2 s .c o m*/ } return data; }