View on GitHub

Devendra's Log

Driving Apache Velocity template engine using XML

The Apache Velocity Runner merges an XML document such as

<!DOCTYPE velocity SYSTEM "velocity.dtd">

<velocity>
    <template root="c:\java\velocity" file="HelloVelocity.txt"/>
    <output root="c:\java\velocity" file="HelloVelocity.out.txt"/>
    <context>
        <property name="Name" value="Devendra Tewari"/>
        <list name="lastaccesses">
            <object>
                <property name="time" value="12/12/2001 13:30"/>
                <property name="computer" value="abc"/>
            </object>
            <object>
                <property name="time" value="31/12/2001 18:30"/>
                <property name="computer" value="abc"/>
            </object>
            <object>
                <property name="time" value="12/12/2001 13:30"/>
                <property name="computer" value="abc"/>
            </object>
            <object>
                <property name="time" value="12/12/2001 13:30"/>
                <property name="computer" value="abc"/>
            </object>
            <object>
                <property name="time" value="16/12/2001 13:30"/>
                <property name="computer" value="abc"/>
            </object>
        </list>
    </context>
</velocity>

With a template such as

Hello $Name!

Last $!{lastaccesses.size()} accesses:
#foreach($access in $lastaccesses)
    ${velocityCount}. $access.time from $access.computer
#end

$!silent

To produce the following output

Hello Devendra Tewari!

Last 5 accesses:
    1. 12/12/2001 13:30 from abc
    2. 31/12/2001 18:30 from abc
    3. 12/12/2001 13:30 from abc
    4. 12/12/2001 13:30 from abc
    5. 16/12/2001 13:30 from abc

The XML document schema is available as a DTD.