List of usage examples for javax.xml.xpath XPathConstants NODESET
QName NODESET
To view the source code for javax.xml.xpath XPathConstants NODESET.
Click Source Link
The XPath 1.0 NodeSet data type.
Maps to Java org.w3c.dom.NodeList .
From source file:Main.java
/** * <p>//from ww w.j a va 2 s. c om * Evaluates the given XPath expression in the context of the given item, with * the expectation of a node set result. * </p> * * @param xpe * An XPath expression. * @param item * A context item. * @return The result of the evaluation as a {@link NodeSet} instance. * @throws XPathExpressionException * if the evaluation fails * @since 1.67.5 * @see XPathConstants#NODESET */ public static NodeList evaluateNodeSet(XPathExpression xpe, Object item) throws XPathExpressionException { return (NodeList) xpe.evaluate(item, XPathConstants.NODESET); }
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;/*from ww w .ja va 2 s. co m*/ }
From source file:com.github.born2snipe.project.setup.cli.maven.AssertXml.java
public static void assertElementDoesExist(String expectedText, File file, String xpathQuery) { try {//from w ww . j a v a 2 s.c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xPath.compile(xpathQuery).evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { String text = nodes.item(i).getTextContent(); if (text.equals(expectedText)) { Assert.fail("We expected to NOT find " + expectedText); } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Get xml nodes by xpath expression, based on node * /*from w w w . j a v a 2s. com*/ * @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
/** * Gets the node list from the given xml file and the given xpath expression. * /*from w w w . j ava 2s.c om*/ * @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(File xml, String xpathExpression) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(xml); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile(xpathExpression); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; return nodes; }
From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.util.IntactSolrUtils.java
public static SchemaInfo retrieveSchemaInfo(SolrServer solrServer) throws IOException { SchemaInfo schemaInfo = new SchemaInfo(); if (solrServer instanceof CommonsHttpSolrServer) { final CommonsHttpSolrServer solr = (CommonsHttpSolrServer) solrServer; final String url = solr.getBaseURL() + "/admin/file/?file=schema.xml"; final GetMethod method = new GetMethod(url); final int code = solr.getHttpClient().executeMethod(method); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/schema/fields/field"; InputStream stream = method.getResponseBodyAsStream(); InputSource inputSource = new InputSource(stream); try {// www .ja va 2s. c o m NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { final String fieldName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(); schemaInfo.addFieldName(fieldName); } } catch (XPathExpressionException e) { e.printStackTrace(); } finally { stream.close(); } } else if (solrServer instanceof HttpSolrServer) { final HttpSolrServer solr = (HttpSolrServer) solrServer; final String url = solr.getBaseURL() + "/admin/file/?file=schema.xml"; final HttpUriRequest method = new HttpGet(url); final HttpResponse response = solr.getHttpClient().execute(method); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/schema/fields/field"; InputStream stream = response.getEntity().getContent(); InputSource inputSource = new InputSource(stream); try { NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { final String fieldName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(); schemaInfo.addFieldName(fieldName); } } catch (XPathExpressionException e) { e.printStackTrace(); } finally { stream.close(); } } else { throw new IllegalArgumentException( "Cannot get schema for SolrServer with class: " + solrServer.getClass().getName()); } return schemaInfo; }
From source file:com.dianping.phoenix.dev.core.tools.scanner.ServiceMetaScanner.java
@Override protected List<ServicePortEntry> doScan(Document doc) throws Exception { List<ServicePortEntry> resList = new ArrayList<ServicePortEntry>(); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//service"); Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) xmlRes; for (int i = 0; i < nodes.getLength(); i++) { String serviceName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(); XPathExpression portExpr = xpath.compile("//service[@name='" + serviceName + "']/port/text()"); Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET); NodeList ports = (NodeList) portRes; int projectPort = -1; if (ports.getLength() >= 1) { projectPort = Integer.parseInt(ports.item(0).getNodeValue()); }//from www.j av a2 s . co m if (projectPort > 0 && StringUtils.isNotBlank(serviceName)) { resList.add(new ServicePortEntry(serviceName, projectPort)); } } return resList; }
From source file:Main.java
/** * Get the W3C NodeList instance associated with the XPath selection * supplied.// ww w . j a v a2 s . c o m * * @param node The document node to be searched. * @param xpath The XPath String to be used in the selection. * @return The W3C NodeList instance at the specified location in the * document, or null. */ public static NodeList getNodeList(Node node, String xpath) { if (node == null) { throw new IllegalArgumentException("null 'document' arg in method call."); } else if (xpath == null) { throw new IllegalArgumentException("null 'xpath' arg in method call."); } try { XPath xpathEvaluater = xPathFactory.newXPath(); if (xpath.endsWith(ELEMENT_NAME_FUNC)) { return (NodeList) xpathEvaluater.evaluate( xpath.substring(0, xpath.length() - ELEMENT_NAME_FUNC.length()), node, XPathConstants.NODESET); } else { return (NodeList) xpathEvaluater.evaluate(xpath, node, XPathConstants.NODESET); } } catch (XPathExpressionException e) { throw new IllegalArgumentException("bad 'xpath' expression [" + xpath + "]."); } }
From source file:com.dianping.maven.plugin.tools.misc.scanner.ProjectMetaScanner.java
@Override protected List<ProjectPortEntry> doScan(Document doc) throws Exception { List<ProjectPortEntry> resList = new ArrayList<ProjectPortEntry>(); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//project"); Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) xmlRes; for (int i = 0; i < nodes.getLength(); i++) { String projectName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(); XPathExpression portExpr = xpath.compile("//project[@name='" + projectName + "']/port/text()"); Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET); NodeList ports = (NodeList) portRes; int projectPort = -1; if (ports.getLength() >= 1) { projectPort = Integer.parseInt(ports.item(0).getNodeValue()); }/*from www. j a v a2s . c o m*/ if (projectPort > 0 && StringUtils.isNotBlank(projectName)) { resList.add(new ProjectPortEntry(projectName, projectPort)); } } return resList; }
From source file:Main.java
public static NodeList getNodeList(String xPathExpression, Node node) throws XPathExpressionException { return (NodeList) xPath.evaluate(xPathExpression, node, XPathConstants.NODESET); }