Reputation:
I'd like to take XML in the format below and load each code record into a domain object in my BootStrap.groovy
. I want to preserve the formatting of each snippet of code.
<records>
<code>
<language>Groovy</language>
<snippet>
println "This is Groovy"
println "A very powerful language"
</snippet>
</code>
<code>
<language>Groovy</language>
<snippet>
3.times {
println "hello"
}
</snippet>
</code>
<code>
<language>Perl</language>
<snippet>
@foo = split(",");
</snippet>
</code>
</records>
Code {
String language
String snippet
}
new Code(language l, snippet: x).save()
Upvotes: 1
Views: 4063
Reputation: 240
If you can specity a DTD or similar and your XML parser obeys it, I think you can specify the contents of the snippet element to be CDATA and always get it as-is.
Upvotes: 0
Reputation: 5165
roughly something like this:
def CODE_XML = '''
<records>
<code>
<language>Groovy</language>
<snippet>
println "This is Groovy"
println "A very powerful language"
</snippet>
</code>
<code>
<language>Groovy</language>
<snippet>
3.times {
println "hello"
}
</snippet>
</code>
<code>
<language>Perl</language>
<snippet>
@foo = split(",");
</snippet>
</code>
</records>
'''
def records = new XmlParser().parseText(CODE_XML)
records.code.each() { code ->
new Code(language: code.language, snippet: code.snippet).save()
}
Upvotes: 1