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
/*** * /*from w w w . j a va2s. c o m*/ * @param is * @param xpathStr * @return * if is is null , return null<br> * @throws SAXException * @throws IOException * @throws XPathExpressionException */ public static String[] getMultiValue(InputStream is, String xpathStr) throws SAXException, IOException, XPathExpressionException { String[] result = null; Document doc = null; if (is == null) return result; doc = dombuilder.parse(is); Object obj = xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODESET); if (obj != null) { NodeList nodes = (NodeList) obj; result = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) result[i] = nodes.item(i).getNodeValue(); } return result; }
From source file:Main.java
/** * Get the XML Nodes for the specified XPath. * /*from www . j a v a2s . c om*/ * @param path * - XPath String * @param parent * - Parent Node. * @return * @throws Exception */ public static NodeList search(String path, Element parent) throws Exception { // XPath Query for showing all nodes value XPathExpression expr = xpath.compile(path); return (NodeList) expr.evaluate(parent, XPathConstants.NODESET); }
From source file:Main.java
public static NodeList getElementsByXpath(Document docInput, String xPath) throws XPathExpressionException { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(xPath); return ((NodeList) expr.evaluate(docInput, XPathConstants.NODESET)); }
From source file:Main.java
public static Element[] getElements(Element parent, String expression) { boolean isSimple = true; for (int i = 0; i < expression.length(); i++) { if (!Character.isAlphabetic(expression.charAt(i))) { isSimple = false;/* ww w .jav a 2s. c o m*/ break; } } if (isSimple) { return getDirectElements(parent, expression); } else { try { XPathExpression xquery; if (cache.containsKey(expression)) { xquery = cache.get(expression); } else { xquery = xpath.compile(expression); cache.put(expression, xquery); } return itemsOf((NodeList) xquery.evaluate(parent, XPathConstants.NODESET)); } catch (XPathExpressionException ex) { return null; } } }
From source file:Main.java
/** * Returns the value of the nodes described by a xpath expression * // w ww . jav a 2 s. co m * @param xml * the xml string * @param xpathExpression * the xpath expression * @return the text value of the nodes described by the xpath expression * @throws XPathExpressionException */ public static List<String> getNodeValues(String xml, String xpathExpression) throws XPathExpressionException { List<String> nodeValues = new ArrayList<String>(); Document doc = parseXml(xml); XPath xpath = xPathFactory.newXPath(); XPathExpression expr = xpath.compile(xpathExpression); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { nodeValues.add(nodes.item(i).getNodeValue()); } return nodeValues; }
From source file:Main.java
public static Element getUniqueElement(Node element, String xpathExpression) { try {// www .j av a 2s .c om NodeList nl = (NodeList) xpath.compile(xpathExpression).evaluate(element, XPathConstants.NODESET); if (nl.getLength() == 0 || nl.getLength() > 1) { throw new RuntimeException("More or less (" + nl.getLength() + ") than 1 element found for expression: " + xpathExpression); } return (Element) nl.item(0); } catch (Exception e) { throw new RuntimeException("Exception evaluating xpath '" + xpathExpression + "' on " + element, e); } }
From source file:Main.java
public static NodeList getNodesByPath(String path, Element localElement, Document doc) throws XPathExpressionException { // Note: if using absolute path, then the root element must also be specified, // that is, it should be like "/clinical_studies/clinical_study/..." XPath xpath = factory.newXPath(); Object element = path.startsWith("/") || localElement == null ? doc : localElement; NodeList nodeList = (NodeList) xpath.evaluate(path, element, XPathConstants.NODESET); return nodeList; }
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;/* ww w .ja va 2s .com*/ 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
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 a v a2s .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); }
From source file:Main.java
/** * Get xml nodes by xpath string, based on xml stream * //from w ww. j a v a 2 s . c om * @param input * @param xpath * @return * @throws Exception */ public static NodeList getNodesByXPath(InputStream input, String xpath) throws Exception { return (NodeList) getXPathExpression(xpath).evaluate(getDocument(input), XPathConstants.NODESET); }