-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxml2codeGenerator.groovy
39 lines (37 loc) · 1007 Bytes
/
xml2codeGenerator.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def text = '''
<list name="example-list">
<technology>
<name>Groovy</name>
</technology>
</list>
'''
def list = new XmlSlurper().parseText(text)
def printMarkUp(list) {
def attr = list.attributes().toString().replace("[", "").replace("]", "")
if (attr.size() == 1) {
attr = "";
} else {
attrVal = attr.split(":");
attr = "("+attrVal[0]+":'"+attrVal[1]+"')";
//println attr
}
//println "node attributes : ${attr.size()}"
if(list.children().size() > 0) {
println "${list.name()}${attr}"
print "{"
list.children().each {
child ->
printMarkUp(child)
}
println "}"
}
else {
if(list.text()?.trim()) {
println "${list.name()}('${list.text()}')"
} else {
if(attr.isEmpty()) attr="()"
println "${list.name()}${attr}"
}
}
}
printMarkUp(list)