List of usage examples for javax.xml.xpath XPathExpression evaluate
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;
From source file:Main.java
/** * Return the Element corresponding the the XPath * //from w ww .j av a2 s . com * @param xmlNode * @param xpathString * @return * @throws XPathExpressionException */ public static Element getElement(Node xmlNode, String xpathString) throws XPathExpressionException { XPathExpression expr = createXPathExpression(xpathString); NodeList list; list = (NodeList) expr.evaluate(xmlNode, XPathConstants.NODESET); if (list.getLength() > 1) { throw new RuntimeException("More than one result for XPath: " + xpathString); } return (Element) list.item(0); }
From source file:Main.java
public static String evaluateStringXPath(final Document document, final XPathExpression expression) throws XPathExpressionException { notNull(document);/* ww w. j a v a 2s .com*/ notNull(expression); return (String) expression.evaluate(document, XPathConstants.STRING); }
From source file:Main.java
/** * Get the searchHandler Node from the solrconfig.xml file * * @param solrconfig/*from w ww .j av a 2 s. c o m*/ * the solrconfig.xml File * * @return searchHandler XML Node or null if not found * * @throws ParserConfigurationException * @throws SAXException * @throws IOException * @throws XPathExpressionException */ public static Node getSearchHandlerNode(final File solrconfig) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); final Document docSchem = dBuilder.parse(solrconfig); final XPathFactory xPathfactory = XPathFactory.newInstance(); final XPath xpath = xPathfactory.newXPath(); final XPathExpression expr = xpath .compile("//requestHandler[@class=\"solr.SearchHandler\" and @name=\"/select\"]"); final Node requestHandler = (Node) expr.evaluate(docSchem, XPathConstants.NODE); return requestHandler; }
From source file:Main.java
@SuppressWarnings("UseSpecificCatch") public static boolean saveXML(Document doc, File outfile, boolean indent) { try {//www .j a va 2s . c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")); if (indent) { XPathFactory xpathFactory = XPathFactory.newInstance(); // XPath to find empty text nodes. XPathExpression xpathExp = xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']"); NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET); // Remove each empty text node from document. for (int i = 0; i < emptyTextNodes.getLength(); i++) { Node emptyTextNode = emptyTextNodes.item(i); emptyTextNode.getParentNode().removeChild(emptyTextNode); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } transformer.transform(source, result); return true; } catch (Exception ex) { return false; } }
From source file:Main.java
public static String xPathSearch(String input, String expression) throws Exception { DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(expression); Document document = documentBuilder.parse(new ByteArrayInputStream(input.getBytes("UTF-8"))); String output = (String) xPathExpression.evaluate(document, XPathConstants.STRING); return output == null ? "" : output.trim(); }
From source file:Main.java
public static NodeList selectNodeList(Node node, String str) throws XPathExpressionException { XPathExpression xPathExpression = null; NodeList nList = null;//from w w w.j a va2s.c om synchronized (getXPathBuilder()) { xPathExpression = getXPathBuilder().compile(str); nList = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET); } return nList; }
From source file:Main.java
public static final String[] executeXPathExpression(InputSource inputSource, String xpathExpression, String namespace) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, TransformerException { // optional namespace spec: xmlns:prefix:URI String nsPrefix = null;//www. ja v a 2s .c om String nsUri = null; if (namespace != null && namespace.startsWith("xmlns:")) { String[] nsDef = namespace.substring("xmlns:".length()).split("="); if (nsDef.length == 2) { nsPrefix = nsDef[0]; nsUri = nsDef[1]; } } // Parse XML to DOM DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); Document doc = dbFactory.newDocumentBuilder().parse(inputSource); // Find nodes by XPATH XPathFactory xpFactory = XPathFactory.newInstance(); XPath xpath = xpFactory.newXPath(); // namespace? if (nsPrefix != null) { final String myPrefix = nsPrefix; final String myUri = nsUri; xpath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { return myPrefix.equals(prefix) ? myUri : null; } public String getPrefix(String namespaceURI) { return null; // we are not using this. } public Iterator<?> getPrefixes(String namespaceURI) { return null; // we are not using this. } }); } XPathExpression expr = xpath.compile(xpathExpression); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); List<String> lines = new ArrayList<>(); for (int i = 0; i < nodes.getLength(); i++) { lines.add((indenting(nodes.item(i)))); } return lines.toArray(new String[lines.size()]); }
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:com.github.radium226.github.maven.MetaDataDownloader.java
public static String evaluateXPath(InputStream pomInputStream, String expression) throws XPathExpressionException { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); xPath.setNamespaceContext(new NamespaceContext() { @Override/* ww w .j a v a 2 s. com*/ public String getNamespaceURI(String prefix) { if (prefix.equals("ns")) { return "http://maven.apache.org/POM/4.0.0"; } return null; } @Override public String getPrefix(String namespaceURI) { return null; } @Override public Iterator getPrefixes(String namespaceURI) { return null; } }); XPathExpression xPathExpression = xPath.compile(expression); String version = (String) xPathExpression.evaluate(new InputSource(pomInputStream), XPathConstants.STRING); return version; }
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);/*ww w .ja va 2 s .c o m*/ 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); }