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
static public ArrayList<Element> selectElements(Element element, String xpathExpression) throws XPathExpressionException { ArrayList<Element> resultVector = new ArrayList<Element>(); if (element == null) { return resultVector; }//from w w w . j av a 2 s .com if (xpathExpression.indexOf("/") == -1) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) { resultVector.add((Element) node); } } } else { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); resultVector.add((Element) node); } } return resultVector; }
From source file:Main.java
/** * Evaluates a XPath expression from a XML node, returning a NodeList * //from ww w .j av a 2 s. c o m * @param expression * A XPath expression * @param node * The NodeList for which this expression should be evaluated * @return The result od evaluating the XPath expression to the given or * <code>null</code> if an ecxception occured NodeList */ public static NodeList evaluateXPathExpressionAndReturnNodeList(String expression, Node node) { try { return (NodeList) xPath.get().evaluate(expression, node, XPathConstants.NODESET); } catch (XPathExpressionException e) { return null; } }
From source file:Main.java
/** * Evaluates the XPath expression in the specified context and returns the found items as a List. * * @param node the XML document to evaluate * @param expression the compiled XPath expression * @return the list of elements found//from ww w . j av a 2s.co m */ public static List<String> getListValue(Node node, XPathExpression expression) { try { NodeList nodeList = (NodeList) expression.evaluate(node, XPathConstants.NODESET); List<String> list = new ArrayList<String>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node item = nodeList.item(i); list.add(item.getFirstChild().getNodeValue()); } return list; } catch (XPathExpressionException e) { // Try to evaluate in string context: String value = getStringValue(node, expression); if (value != null) { List<String> list = new ArrayList<String>(1); list.add(value); return list; } return Collections.emptyList(); } }
From source file:Main.java
/** * Get xml nodes by xpath string, based on xml string * // ww w . j a va 2 s . c om * @param xml * @param xpath * @return * @throws Exception */ public static NodeList getNodesByXPath(String xml, String xpath) throws Exception { return (NodeList) getXPathExpression(xpath).evaluate(getDocument(xml), XPathConstants.NODESET); }
From source file:Main.java
/** * Return the Element corresponding the the XPath * /*w ww . j a v a 2s. c om*/ * @param xmlNode * @param xpathString * @return * @throws XPathExpressionException */ public static NodeList getNodeList(Node xmlNode, String xpathString) { try { XPathExpression expr = createXPathExpression(xpathString); return (NodeList) expr.evaluate(xmlNode, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static NodeList getXPathNodes(String search, Document domDocument, XPath xpathInstance) { try {// www . j av a 2s . c o m XPathExpression expr = xpathInstance.compile(search); return (NodeList) expr.evaluate(domDocument, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Return the Element corresponding the the XPath * //from w ww.j a v a 2 s . c o m * @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
@SuppressWarnings("UseSpecificCatch") public static boolean saveXML(Document doc, File outfile, boolean indent) { try {// w w w . j a v a 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:eu.planets_project.tb.utils.XCDLParser.java
/** * /* w w w .j av a 2s . c o m*/ * @param xcdlDoc * @return * @throws XPathExpressionException */ public static List<MeasurementImpl> parseXCDL(Document xcdlDoc) throws XPathExpressionException { List<MeasurementImpl> props = new Vector<MeasurementImpl>(); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("/*//property", xcdlDoc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); String name = (String) xpath.evaluate("./name", n, XPathConstants.STRING); String id = (String) xpath.evaluate("./name/@id", n, XPathConstants.STRING); // Loop through the property definitions and patch them into Property objects. MeasurementImpl m = new MeasurementImpl(makePropertyUri(id, name), (String) xpath.evaluate("./valueSet/labValue/val", n, XPathConstants.STRING)); // FIXME Unify this construction: See also XCDLService.createPropertyFromFFProp // m.setType( "xcdl:" + (String) xpath.evaluate( "./valueSet/labValue/type", n, XPathConstants.STRING) ); props.add(m); } return props; }
From source file:Main.java
/** * Checks in under a given root element whether it can find a child elements * which match the XPath expression supplied. Returns a {@link List} of * {@link Element} if they exist./* w w w. j ava 2 s . c o m*/ * * Please note that the XPath parser used is NOT namespace aware. So if you * want to find a element <beans><sec:http> you need to use the following * XPath expression '/beans/http'. * * @param xPathExpression the xPathExpression * @param root the parent DOM element * * @return a {@link List} of type {@link Element} if discovered, otherwise null */ public static List<Element> findElements(String xPathExpression, Element root) { List<Element> elements = new ArrayList<Element>(); NodeList nodes = null; try { XPathExpression expr = xpath.compile(xPathExpression); nodes = (NodeList) expr.evaluate(root, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new IllegalArgumentException("Unable evaluate xpath expression", e); } for (int i = 0; i < nodes.getLength(); i++) { elements.add((Element) nodes.item(i)); } return elements; }