I have some test config in an XML doc like this:
<WapRules>
<WapRule>
<RuleTypeId>1</RuleTypeId>
<IsExact>false</IsExact>
<Keywords>
<Keyword>gmail</Keyword>
</Keywords>
</WapRule>
<WapRule>
<RuleTypeId>2</RuleTypeId>
<IsExact>false</IsExact>
<Keywords>
<Keyword>test</Keyword>
</Keywords>
</WapRule>
<WapRule>
<RuleTypeId>2</RuleTypeId>
<IsExact>true</IsExact>
<Keywords>
<Keyword>srs</Keyword>
<Keyword>sample</Keyword>
</Keywords>
</WapRule>
</WapRules>
I want to represent this as a groovy config and then slurp it in.
“WapRules” is a list of “WapRule” elements, so I tried to map this to a Config.groovy file like this:
wap_rules = [
{
rule_type_id = 1
is_exact = false
keywords = ["gmail"]
},
{
rule_type_id = 2
is_exact = false
keywords = ["test"]
},
{
rule_type_id = 2
is_exact = true
keywords = ["srs", "sample"]
}
]
and now I slurp in the config and try to access the elements:
def cfg = new ConfigSlurper().parse(Config)
println cfg.wap_rules[0].rule_type_id
but the output just has this: [:]
So how can I access the members inside cfg.wap_rules[0] ? Is there something wrong with my mapping of XML structure to Config.groovy?
Got the answer. I wasn’t mapping the XML document properly.
Here’s how I should have written my Config.groovy:
wap_rulesis now a list of maps, and each map’s elements can be accessed easily.Now I can indeed do
println cfg.wap_rules[0].rule_type_idand the output is indeed1I guess I was too conditioned by JSON representation (i.e. “I need a list of JSON objects, each one in braces {}”) when I wrote the erroneous Config.groovy.
I hope this helps someone else.