Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2013-2014 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.integration.xml.xpath;
 import java.util.List;
Utility class for 'xpath' support.

Artem Bilan
 public final class XPathUtils {
 	public static final String STRING = "string";
 	public static final String BOOLEAN = "boolean";
 	public static final String NUMBER = "number";
 	public static final String NODE = "node";
 	public static final String NODE_LIST = "node_list";
 	public static final String DOCUMENT_LIST = "document_list";
 	private static List<StringRESULT_TYPES = Arrays.asList();
 	private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

Utility method to evaluate an xpath on the provided object. Delegates evaluation to an XPathExpression. Note this method provides the #xpath() SpEL function.

o the xml Object for evaluaton.
xpath an 'xpath' expression String.
resultArg an optional parameter to represent the result type of the xpath evaluation. Only one argument is allowed, which can be an instance of org.springframework.xml.xpath.NodeMapper or one of these String constants: "string", "boolean", "number", "node" or "node_list".
<T> The required return type.
the result of the xpath expression evaluation.
IllegalArgumentException - if the provided arguments aren't appropriate types or values;
MessagingException - if the provided object can't be converted to a Node;
XPathException - if the xpath expression can't be evaluated.
 	public static <T> T evaluate(Object oString xpathObject... resultArg) {
 		Object resultType = null;
 		if (resultArg != null && resultArg.length > 0) {
 			Assert.isTrue(resultArg.length == 1, "'resultArg' can contains only one element.");
 			Assert.noNullElements(resultArg"'resultArg' can't contains 'null' elements.");
 			resultType = resultArg[0];
 		XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpath);
 		Node node = .convertToNode(o);
 		if (resultType == null) {
 			return (T) expression.evaluateAsString(node);
 		else if (resultType instanceof NodeMapper<?>) {
 			return (T) expression.evaluateAsObject(node, (NodeMapper<?>) resultType);
		else if (resultType instanceof String && .contains(resultType)) {
			String resType = (StringresultType;
			if (.equals(resType)) {
				List<NodenodeList = (List<Node>) ..evaluateXPath(expressionnode);
				try {
					List<Nodedocuments = new ArrayList<Node>(nodeList.size());
					for (Node n : nodeList) {
						Document document = documentBuilder.newDocument();
					return (T) documents;
					throw new XPathException("Unable to create 'documentBuilder'."e);
			else {
				XPathEvaluationType evaluationType = XPathEvaluationType.valueOf(resType.toUpperCase() + "_RESULT");
				return (T) evaluationType.evaluateXPath(expressionnode);
		else {
			throw new IllegalArgumentException("'resultArg[0]' can be an instance of 'NodeMapper<?>' " +
					"or one of supported String constants: " + );
	private XPathUtils() {
New to GrepCode? Check out our FAQ X