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
/** * Returns the unique XML element matching the given {@code xpathExpression} * * @param node/*from ww w . ja va 2 s .c o m*/ * @param xpathExpression xpath expression to find the node * @return the matching node * @throws RuntimeException 0 or more than 1 matching element found */ @Nonnull public static Element getUniqueElement(@Nonnull Node node, @Nonnull String xpathExpression) { try { NodeList nl = (NodeList) xpath.compile(xpathExpression).evaluate(node, XPathConstants.NODESET); if (nl.getLength() == 0 || nl.getLength() > 1) { throw new RuntimeException("More or less (" + nl.getLength() + ") than 1 node found for expression: " + xpathExpression); } return (Element) nl.item(0); } catch (Exception e) { throw new RuntimeException("Exception evaluating xpath '" + xpathExpression + "' on " + node, e); } }
From source file:Main.java
/** * Get xml nodes by xpath string , based on node * /*from w ww.j a v a2 s. c o m*/ * @param node * @param xpath * @return * @throws Exception */ public static NodeList getNodesByXPath(Node node, String xpath) throws Exception { return (NodeList) getXPathExpression(xpath).evaluate(node, XPathConstants.NODESET); }
From source file:Main.java
public static NodeList getNodeList(Node node, String path) { try {//from w ww . j a v a2 s . c o m slowGetValue++; // System.out.println("slow : " + slowGetValue + " - " + path); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression expr; expr = xpath.compile(path); NodeList list = (NodeList) expr.evaluate(node, XPathConstants.NODESET); return list; } catch (XPathExpressionException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Evaluates the XPath expression against the <code>xml</code> and returns the selected nodes. * /*from w ww .j a v a 2 s . com*/ * @param expression Expression to evaluate. * @param xml The xml to query. * @return The selected nodes. * @throws XPathExpressionException If an error occurs evaluating the expression. */ public static NodeList getNodes(final String expression, final String xml) throws XPathExpressionException { final InputSource source = new InputSource(new StringReader(xml)); final XPathFactory factory = XPathFactory.newInstance(); final XPath xpath = factory.newXPath(); return (NodeList) xpath.evaluate(expression, source, XPathConstants.NODESET); }
From source file:Main.java
/** * getStringListFromXPath/* w w w .j a va 2s . co m*/ * Gets a list of strings from an xml. * @param rootNode The root node to perform the listExpression on. * @param listExpression Evaluated on the rootNode, gets a NodeList. * @return A list of the text contents of the nodes returned by evaluating the listExpression. */ public static List<String> getStringListFromXPath(Node rootNode, XPath xpath, String listExpression) { synchronized (xpath) { if (rootNode instanceof Document) rootNode = ((Document) rootNode).getDocumentElement(); Vector<String> result = new Vector<String>(); try { NodeList nodes = (NodeList) xpath.evaluate(listExpression, rootNode, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { result.addElement(nodes.item(i).getTextContent()); } } catch (Exception e) { System.err.println("Error evaluating xpath expression: " + listExpression); e.printStackTrace(); } return result; } }
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);/*w w w . j av a2 s . com*/ 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:org.apache.solr.kelvin.responseanalyzers.LegacyResponseAnalyzer.java
public void decode(Map<String, Object> previousResponses) throws Exception { ObjectMapper mapper = new ObjectMapper(); ArrayNode response = mapper.createArrayNode(); if (!previousResponses.containsKey(XmlResponseAnalyzer.XML_DOM)) { previousResponses.put(XmlDoclistExtractorResponseAnalyzer.DOC_LIST, response); //empty return;/*from w ww. jav a2 s . co m*/ } NodeList nodeList = (NodeList) expr.evaluate((Document) previousResponses.get(XmlResponseAnalyzer.XML_DOM), XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { Node doc = nodeList.item(i); ObjectNode oDoc = mapper.createObjectNode(); Node subel = doc.getFirstChild(); while (subel != null) { String localName = subel.getNodeName(); if (localName == "field") { String fieldName = subel.getAttributes().getNamedItem("name").getNodeValue(); if (!"infoold".equals(fieldName)) { String value = subel.getTextContent(); oDoc.put(fieldName, value); } } else if (localName == "slug_preferenziale") { NodeList slugNodes = subel.getChildNodes(); oDoc.put("slug_preferenziale", slugNodes.item(slugNodes.getLength() - 1).getAttributes() .getNamedItem("path").getTextContent()); } subel = subel.getNextSibling(); } response.add(oDoc); } previousResponses.put(XmlDoclistExtractorResponseAnalyzer.DOC_LIST, response); }
From source file:Main.java
private static NodeList findElements(String xpathExpression, Document doc, XPath xpath) throws Exception { NodeList nodes = null;/*from ww w.j av a2s. co m*/ if (doc != null) { XPathExpression expr = xpath.compile(xpathExpression); Object result = expr.evaluate(doc, XPathConstants.NODESET); nodes = (NodeList) result; } return nodes; }
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;//from ww w. jav a 2 s . c o m 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:org.apache.solr.kelvin.responseanalyzers.XmlDoclistExtractorResponseAnalyzer.java
public void decode(Map<String, Object> previousResponses) throws Exception { ObjectMapper mapper = new ObjectMapper(); ArrayNode response = mapper.createArrayNode(); if (!previousResponses.containsKey(XmlResponseAnalyzer.XML_DOM)) { previousResponses.put(DOC_LIST, response); //empty return;// w w w . j av a 2 s . com } NodeList nodeList = (NodeList) expr.evaluate((Document) previousResponses.get(XmlResponseAnalyzer.XML_DOM), XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { Node doc = nodeList.item(i); ObjectNode oDoc = mapper.createObjectNode(); Node subel = doc.getFirstChild(); while (subel != null) { if (subel.getNodeType() != Node.TEXT_NODE) { String fieldName = subel.getAttributes().getNamedItem("name").getNodeValue(); String elementName = subel.getNodeName(); if ("arr".equals(elementName)) { ArrayNode multivaluedField = mapper.createArrayNode(); Node mvItem = subel.getFirstChild(); while (mvItem != null) { multivaluedField.add(mvItem.getTextContent()); mvItem = mvItem.getNextSibling(); } oDoc.put(fieldName, multivaluedField); } else { String value = subel.getTextContent(); oDoc.put(fieldName, value); } } subel = subel.getNextSibling(); } response.add(oDoc); } previousResponses.put(DOC_LIST, response); }