List of usage examples for javax.xml.xpath XPathExpression evaluate
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;
From source file:Main.java
/** Evaluates an XPath. */ private static synchronized Object evalXPath(String expression, Object item, QName type) { if (xPath == null) xPath = XPathFactory.newInstance().newXPath(); try {/*from w w w. j ava 2s . c o m*/ XPathExpression exp = xPath.compile(expression); Object node = exp.evaluate(item, XPathConstants.NODE); return (node == null) ? null : exp.evaluate(item, type); } catch (XPathExpressionException e) { throw new AssertionError("Error initializing XPath instance for '" + expression + "': " + e); } }
From source file:Main.java
public static String getProcessIdFromBpmn(final String bpmn) { String processId = null;/* w w w . j a v a2 s .c o m*/ try { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); final Document doc = domFactory.newDocumentBuilder() .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8"))); final XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new NamespaceContext() { @Override public Iterator<?> getPrefixes(final String namespaceURI) { // Not used in this context. return null; } @Override public String getPrefix(final String namespaceURI) { // Not used in this context. return null; } @Override public String getNamespaceURI(final String prefix) { // Only require the URI for the bpmn2 NS. return BPMN2_NAMESPACE_URI; } }); final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR); final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE); processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue(); } catch (final Exception e) { e.printStackTrace(); } return processId; }
From source file:Main.java
/** * Evaluates the XPath expression in the specified context and returns the found items as a List. * * @param node the XML document to evaluate * @param expression the compiled XPath expression * @return the list of elements found/*from w ww . j av a 2 s .c o m*/ */ public static List<String> getListValue(Node node, XPathExpression expression) { try { NodeList nodeList = (NodeList) expression.evaluate(node, XPathConstants.NODESET); List<String> list = new ArrayList<String>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node item = nodeList.item(i); list.add(item.getFirstChild().getNodeValue()); } return list; } catch (XPathExpressionException e) { // Try to evaluate in string context: String value = getStringValue(node, expression); if (value != null) { List<String> list = new ArrayList<String>(1); list.add(value); return list; } return Collections.emptyList(); } }
From source file:Main.java
/** * Get xml nodes by xpath expression//from ww w .j a va 2 s. c o m * * @param doc * @param expr * @return * @throws Exception */ public static NodeList getNodesByXPath(Document doc, XPathExpression expr) throws Exception { return (NodeList) expr.evaluate(doc, XPathConstants.NODESET); }
From source file:Main.java
/** * Get xml nodes by xpath expression, based on node * /*from ww w . j av a 2 s.c om*/ * @param node * @param xpath * @return * @throws Exception */ public static NodeList getNodesByXPath(Node node, XPathExpression xpath) throws Exception { return (NodeList) xpath.evaluate(node, XPathConstants.NODESET); }
From source file:Main.java
private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) throws Exception { List<String> list = new ArrayList<>(); XPathExpression expr = xpath.compile("/Employees/Employee[gender='Female']/name/text()"); // evaluate expression result on XML document NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); return list;//from w w w . j a v a2 s . c o m }
From source file:Main.java
/** * Read application-context file, and return fully qualified class name for * given <code>beanName</code> * //from ww w . j av a 2s . c om * @param beanName * @return * */ public static String getFullyQualifiedClass(String beanName) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); String nodeValue = ""; try { DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = builder.parse("application-context.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//bean[@name='" + beanName + "']/@class"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; if (nodes.getLength() > 0) { nodeValue = nodes.item(0).getNodeValue(); } } catch (ParserConfigurationException parserConfigurationException) { parserConfigurationException.printStackTrace(); } catch (IOException ioException) { ioException.printStackTrace(); } catch (SAXException saxException) { saxException.printStackTrace(); } catch (XPathExpressionException xPathExpressionException) { xPathExpressionException.printStackTrace(); } return nodeValue; }
From source file:Main.java
public static String setValueXPath(String srcXmlString, String xPath, String newVal) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // never forget this! int i, j;/*from w ww .j a v a 2s .c om*/ Document doc = null; DocumentBuilder builder = null; 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()); logger.debug(xPathNodes.item(0).getNodeName() + " = " + xPathNodes.item(0).getTextContent()); // get list of all nodes in doc NodeList nodes = doc.getElementsByTagName("*"); // iterate through all the nodes for (i = 0; i < xPathNodes.getLength(); i++) { // for each node in xpath result - traverse through all nodes in // doc to find match for (j = 0; j < nodes.getLength(); j++) { if (nodes.item(j).isSameNode(xPathNodes.item(i))) { logger.debug("Old value " + i + ": " + xPathNodes.item(i).getNodeName() + " = " + xPathNodes.item(i).getTextContent()); nodes.item(j).setTextContent(newVal); logger.debug("New value " + i + ": " + xPathNodes.item(i).getNodeName() + " = " + xPathNodes.item(i).getTextContent()); break; } } } } catch (Exception ex) { logger.error(ex.getMessage()); // ex.printStackTrace(); } return getW3CXmlFromDoc(doc); }
From source file:Main.java
/** * Gets the node list from the given xml file and the given xpath expression. * //from w w w. j av a 2 s . c o m * @param xml * the xml * @param xpathExpression * the xpath expression * @return the node list * @throws XPathExpressionException * the x path expression exception * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static NodeList getNodeList(final File xml, final String xpathExpression) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); final DocumentBuilder builder = domFactory.newDocumentBuilder(); final Document doc = builder.parse(xml); final XPath xpath = XPathFactory.newInstance().newXPath(); final XPathExpression expr = xpath.compile(xpathExpression); final Object result = expr.evaluate(doc, XPathConstants.NODESET); final NodeList nodes = (NodeList) result; return nodes; }
From source file:Main.java
/** * Gets the node list from the given xml file and the given xpath expression. * /* w ww. j av a2s.c o m*/ * @param xml * the xml file as string. * @param xpathExpression * the xpath expression as string. * @return the node list * @throws XPathExpressionException * the x path expression exception * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static NodeList getNodeList(final String xml, final String xpathExpression) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); final DocumentBuilder builder = domFactory.newDocumentBuilder(); final Document doc = builder.parse(xml); final XPath xpath = XPathFactory.newInstance().newXPath(); final XPathExpression expr = xpath.compile(xpathExpression); final Object result = expr.evaluate(doc, XPathConstants.NODESET); final NodeList nodes = (NodeList) result; return nodes; }