Take the 2-minute tour ×

i have a velocity template file which has the data from xml. I want to convert the string into integer type. can anyone help me to find out the solution.

share|improve this question
you have an XSLT file that operates on an XML input document and you want to convert a field that is type xs:string into type xs:integer? –  vicatcu Jan 28 '10 at 17:30
yes i have the xml input doucument which has the string value and i want to convert into integer –  uma Jan 28 '10 at 18:23
why did you not accept the answer? –  Surasin Tancharoen Dec 21 '11 at 11:41

2 Answers 2

up vote 42 down vote accepted

Aha! Been there.

#set($intString = "9")
#set($Integer = 0)

Doing this uses the java underlying velocity. The $Integer variable is nothing more that a java Integer object which you can then use to access .parseInt

share|improve this answer
hi thanks its working fine. –  uma Jan 29 '10 at 16:28
Glad I could help! –  Quotidian Jan 29 '10 at 17:03
Thanks, works fine and cured the headache. –  Matt May 26 '10 at 12:21
Thanks so much for this. I was breaking my head over why $obj.getById("23") works where as #set($id = "23") $obj.getById($id)) doesn't. –  adarshr Apr 24 '12 at 13:59

If you have some control over the velocity context, here's an alternative that alleviates the need to set a variable in the Velocity template.

Context velocityContext = new Context();
velocityContext.put(Integer.class.getSimpleName(), Integer.class);

This allows you to call the static methods of java.lang.Integer in your template using $Integer.parseInt($value) and doesn't rely upon the #set having been called prior to performing the type conversion in the template.

share|improve this answer

Your Answer


