Tharaka Deshan
Tharaka Deshan

Reputation: 1396

How to use inner loops in Apache Velocity to parse XML

I am going to develop a eclipse plugin to generate simple Java source codes. Initially I need to convert a XML file to Java Class code. I found Apache Velocity from internet and now I am playing with some simple stuffs. This is my input XML file:

<?xml version= "1.0" encoding= "UTF-8" ?> 
<Content> 
<Class name= "Customer"  acc_modif="public"> 
<attributes>
 <attribute attribute_type= "String" attribute_name= "studentName"  attribute_acc_modifer="public"/> 
 <attribute attribute_type= "int"  attribute_name= "age"  attribute_acc_modifer="public"/> 
</attributes>

<constructors>
    <constructor con_acc_modifer="public" con_name="Student">

            <para para_type="String" para_name="studentName"/>
            <para para_type="int" para_name="age"/>
    </constructor>

    <constructor con_acc_modifer="public" con_name="Student">

            <para para_type="String" para_name="studentName"/>
            <para para_type="int" para_name="age"/>

    </constructor>
</constructors>
</Class> 

</Content>

And this is my template file:

## class .vm 
##apachi-3

import java.util.*; 

$class.acc_modif class $class.name { 

#foreach ( $att in $class.attributes ) 
   $att.attribute_acc_modifer $att.attribute_type $att.attribute_name; 
#end

#foreach ( $con in $class.constructors ) 
   $con.con_acc_modifer $con.con_name ( $con.para_type $con.para_name ) { ## Not working :(
}       
#end 
}

And the output was like this:

import java.util.*; 

public class Customer { 

public String studentName; 
public int age; 

public Student ( $con.para_type $con.para_name ) {   }       
public Student ( $con.para_type $con.para_name ) {   }       

}

Student constructors are not working. I think I need to use another loop inside brackets instead of "$con.para_type $con.para_name" But I don't know how to use another loop here (i am a noob of Apache Velocity).
Velocity Gurus please help me!

Upvotes: 0

Views: 1649

Answers (1)

duffymo
duffymo

Reputation: 308743

See how you have a <constructors> tag with <constructor> children underneath it?

You need a <paras> parent for your <para> children. You'll have a tough time without it.

Once you have that, just repeat the trick you've already performed.

I'd wonder why you need this when JAXB is part of the JDK. I'd also wonder why simply writing the class would be such a difficult thing to do. Is this one of those cases where you decide that an "automated" solution would be better, but then the time that it takes to figure out how to do it greatly exceeds what you would have spent if you'd simply sat down and started typing?

<?xml version= "1.0" encoding= "UTF-8" ?> 
<Content> 
<Class name= "Customer"  acc_modif="public"> 
<attributes>
 <attribute attribute_type= "String" attribute_name= "studentName"  attribute_acc_modifer="public"/> 
 <attribute attribute_type= "int"  attribute_name= "age"  attribute_acc_modifer="public"/> 
</attributes>

<constructors>
    <constructor con_acc_modifer="public" con_name="Student">
        <parameters>
            <para para_type="String" para_name="studentName"/>
            <para para_type="int" para_name="age"/>
        </parameters>
    </constructor>

    <constructor con_acc_modifer="public" con_name="Student">
        <parameters>
            <para para_type="String" para_name="studentName"/>
            <para para_type="int" para_name="age"/>
        </parameters>
    </constructor>
</constructors>
</Class> 

</Content>

I'd say that your use of upper and lower case is inconsistent. Pick one and stick with it.

And your template:

## class .vm 
##apachi-3

import java.util.*; 

$class.acc_modif class $class.name { 

#foreach ( $att in $class.attributes ) 
   $att.attribute_acc_modifer $att.attribute_type $att.attribute_name; 
#end

#foreach ( $con in $class.constructors ) 
   $con.con_acc_modifer $con.con_name ( 
   #foreach ($parameter in $parameters) # Not 100% sure of notation; I'll leave that for you
       $con.para_type, $con.para_name
   #end  
   ) {
}       
#end 
}

Upvotes: 2

Related Questions