Skip to content

Instantly share code, notes, and snippets.

@narkisr
Created April 11, 2010 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narkisr/362919 to your computer and use it in GitHub Desktop.
Save narkisr/362919 to your computer and use it in GitHub Desktop.
class SpeculationDTO {
private int value1;
private double value2;
}
def xml = """
<Attributes>
<!-- Configuration -->
<Attribute>
<name>value1</name>
<value>4</value>
</Attribute>
<Attribute>
<name>value2</name>
<value>1.02</value>
</Attribute>
</Attributes>
"""
def convert = {
def parsedData = new XmlSlurper().parseText(xml);
def speculation = new SpeculationDTO();
//for each attribute, call the propriate setter method in 'SpeculationDTO'
for(i in parsedData.Attribute) {
speculation."$i.name" = speculation."$i.name" instanceof Number ? speculation."$i.name".class.newInstance(i.value.text()) :i.value.text()
}
return speculation;
}
r = convert()
println "${r.value1} ${r.value2}"
@eranmiz132
Copy link

public class SpeculationDTO {
//configuration
private int value1;

private double value2;

}

def xml = '''



value1
4


value2
1.02



'''

def convert = {
def parsedData = new XmlSlurper().parseText(xml);

    def speculation = new SpeculationDTO();

    //for each attribute, call the propriate setter method in 'SpeculationDTO'
    for(i in parsedData.Attributes.Attribute) {
        speculation."$i.name" = i.value.text();
    }

    return speculation;

}
convert()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment