List of usage examples for javax.xml.xpath XPath compile
public XPathExpression compile(String expression) throws XPathExpressionException;
From source file:Main.java
/** * Uses the passed XPath expression to locate a set of nodes in the passed element. * @param targetNode The node to search. * @param expression The XPath expression. * @return A list of located nodes./*from w w w. j ava 2 s . com*/ */ public static List<Node> xGetNodes(Node targetNode, String expression) { List<Node> nodes = new ArrayList<Node>(); XPath xpath = null; try { xpath = XPathFactory.newInstance().newXPath(); XPathExpression xpathExpression = xpath.compile(expression); NodeList nodeList = (NodeList) xpathExpression.evaluate(targetNode, NODESET); if (nodeList != null) { for (int i = 0; i < nodeList.getLength(); i++) { nodes.add(nodeList.item(i)); } } return nodes; } catch (Exception e) { throw new RuntimeException("XPath:Failed to locate the nodes:" + expression, e); } }
From source file:Main.java
/** * Extract the parameters specified in the XML document for the Element * specified by parent.//from ww w .j a va 2 s . c o m * * @param parent * - Parent Node. * @return * @throws Exception */ public static HashMap<String, Object> parseParams(Element parent) throws Exception { XPath xpath = XPathFactory.newInstance().newXPath(); // XPath Query for showing all nodes value XPathExpression expr = xpath.compile(_PARAM_NODE_PARAMS_); NodeList nodes = (NodeList) expr.evaluate(parent, XPathConstants.NODESET); if (nodes != null && nodes.getLength() > 0) { HashMap<String, Object> params = new HashMap<String, Object>(); for (int ii = 0; ii < nodes.getLength(); ii++) { Element elm = (Element) nodes.item(ii); String name = elm.getAttribute(_PARAM_ATTR_NAME_); String value = elm.getAttribute(_PARAM_ATTR_VALUE_); if (name != null && !name.isEmpty()) { params.put(name, value); } } return params; } return null; }
From source file:Main.java
public static boolean updateDBConfig(String userName, String password, String name, String driver, String url, String dbConfigPath) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException { Document document = loadXML(dbConfigPath); XPath path = XPathFactory.newInstance().newXPath(); XPathExpression express = path.compile( "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Set[@name='className']"); NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); Node node = nodes.item(0);/*w w w. j av a2 s . com*/ node.setTextContent(driver); path.reset(); express = path.compile( "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Get['@name=driverProperties']/Put"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if ("user".equals(node.getAttributes().item(0).getTextContent())) { node.setTextContent(userName); } else if ("password".equals(node.getAttributes().item(0).getTextContent())) { node.setTextContent(password); } else if ("URL".equals(node.getAttributes().item(0).getTextContent())) { // String url = node.getTextContent(); // System.out.println("basic url ---> " + url + "; name -->" + name); // url = replaceDBName("examples", name, url); // System.out.println("dist url ---> " + url); node.setTextContent(url); } } path.reset(); return updateXML(document, dbConfigPath); }
From source file:Main.java
/** * Returns the xml value.//from w w w.j a va 2 s . c o m * * @param sIn * @param sxpath * @return * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws XPathExpressionException */ public static String XPathValueFromString(String sIn, String sxpath) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { // DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); Document doc = loadXMLFromString(sIn); XPath xPath = XPathFactory.newInstance().newXPath(); // XPath Query for showing all nodes value XPathExpression expr = xPath.compile(sxpath); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String sReturn = ""; for (int i = 0; i < nodes.getLength(); i++) { sReturn = nodes.item(i).getNodeValue(); } return sReturn; }
From source file:Main.java
public static boolean updatePomDBConfig(String userName, String password, String name, String host, String port, String version, String pomPath) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException { Document document = loadXML(pomPath); XPath path = XPathFactory.newInstance().newXPath(); XPathExpression express = path.compile("//project/properties/mysql.server.version"); NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); Node node = nodes.item(0);//from w w w. j av a 2 s . c om node.setTextContent(version); path.reset(); express = path.compile("//project/properties/mysql.server.host"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(host); path.reset(); express = path.compile("//project/properties/mysql.server.port"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(port); path.reset(); express = path.compile("//project/properties/mysql.server.database"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(name); path.reset(); express = path.compile("//project/properties/mysql.server.user"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(userName); path.reset(); express = path.compile("//project/properties/mysql.server.password"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(password); path.reset(); return updateXML(document, pomPath); }
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));/*from w w w.ja va 2s. com*/ } return data; }
From source file:Main.java
/** * Returns a compiled XPath expression.//ww w. j a va2 s.c om * * @param expression The XPath expression as a String. * @param namespaceContext The namespace context used by the XPath expression. * @return The compiled XPathExpression. * @throws XPathExpressionException If a parsing error occurs. */ public static XPathExpression compile(String expression, NamespaceContext namespaceContext) throws XPathExpressionException { XPath compiler = XPathFactory.newInstance().newXPath(); if (namespaceContext != null) compiler.setNamespaceContext(namespaceContext); return compiler.compile(expression); }
From source file:com.wso2telco.identity.application.authentication.endpoint.util.ReadMobileConnectConfig.java
public static Map<String, String> query(String XpathExpression) { Map<String, String> ConfigfileAttributes = new Hashtable<String, String>(); // standard for reading an XML file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w. j av a 2 s. c om DocumentBuilder builder; Document doc = null; XPathExpression expr = null; try { builder = factory.newDocumentBuilder(); doc = builder.parse(CarbonUtils.getCarbonConfigDirPath() + File.separator + "mobile-connect.xml"); // create an XPathFactory XPathFactory xFactory = XPathFactory.newInstance(); // create an XPath object XPath xpath = xFactory.newXPath(); // compile the XPath expression expr = xpath.compile("//" + XpathExpression + "/*"); // run the query and get a nodeset Object result = expr.evaluate(doc, XPathConstants.NODESET); // // cast the result to a DOM NodeList NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { ConfigfileAttributes.put(nodes.item(i).getNodeName(), nodes.item(i).getTextContent()); } } catch (SAXException e) { log.error(e.getMessage()); } catch (IOException e) { log.error(e.getMessage()); } catch (ParserConfigurationException e) { log.error(e.getMessage()); } catch (XPathExpressionException e) { log.error(e.getMessage()); } return ConfigfileAttributes; }
From source file:es.upv.grycap.coreutils.fiber.test.mockserver.ObjectResponseValidator.java
@Nullable private static String readObjectIdFromXml(final String payload) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(payload.getBytes())); final XPath xPath = XPathFactory.newInstance().newXPath(); final XPathExpression xPathExpression = xPath.compile("//*/coreutils/objectId/text()"); return trimToNull((String) xPathExpression.evaluate(document, STRING)); }
From source file:Main.java
public static String getAttribute(String fileName, String xPathExpression, String attributeName) { try {/*w w w. ja v a 2s. c o m*/ Document document = parseXML(new File(fileName)); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression xpathExpression = xpath.compile(xPathExpression); Node node = (Node) xpathExpression.evaluate(document, NODE); return getAttributeValueByName(node, attributeName); } catch (Exception e) { throw new RuntimeException("Failed to extract element from:" + fileName, e); } }