Created
April 11, 2010 17:52
-
-
Save narkisr/362919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class SpeculationDTO {
//configuration
private int value1;
}
def xml = '''
value1
4
value2
1.02
'''
def convert = {
def parsedData = new XmlSlurper().parseText(xml);
}
convert()