1

I have string like Color=Blue|Size=M|Style=simpleStyle and it need to be converted as like below by using groovy.

<Item>
      <comp>
        <name>Color</name>
        <value>Blue</value>
      </comp>
      <comp>
        <name>Size</name>
        <value>M</value>
      </comp>
      <comp>
        <name>Style</name>
        <value>simpleStyle</value>
      </comp>
</Item>

I have written groovy for loop something like below. I believe i am trying it in bit harder way. Is there any simple way to produce above XML using Groovy ?

for (int i = 0; StrRelationshipDetails.toString().contains('|'); i++) {
    println StrRelationshipDetails.toString()

    def StrPair = new StringBuilder(StrRelationshipDetails.substring(0, StrRelationshipDetails.indexOf('|')))
    def StrName = new StringBuilder(StrPair.substring(0, StrPair.indexOf('=')))
    def StrValue = new StringBuilder(StrPair.substring(StrPair.indexOf('=')+1, StrPair.size()))

    StrRelationshipDetails = StrRelationshipDetails.substring(StrRelationshipDetails.indexOf('|')+1, StrRelationshipDetails.size())
}
    println StrRelationshipDetails.toString()
Simbu
  • 766
  • 1
  • 12
  • 37

2 Answers2

3

This way (a Groovy one) should work without the use of for loop:

String provided = "Color=Blue|Size=M|Style=simpleStyle"

def builder = new groovy.xml.MarkupBuilder()

builder.Item {
    provided.tokenize('|')*.tokenize('=').each { element ->
        comp {
            name element[0]
            value element[1]
        }
    }
}

return

results in

<Item>
  <comp>
    <name>Color</name>
    <value>Blue</value>
  </comp>
  <comp>
    <name>Size</name>
    <value>M</value>
  </comp>
  <comp>
    <name>Style</name>
    <value>simpleStyle</value>
  </comp>
</Item>
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
0

I would do something like

def result = "<Item>"
def values = StrRelationshipDetails.toString().tokenize("|")
for(def kv of values) {
    result +="<comp>"
    def pair = kv.tokenize("=")
    def name = pair[0]
    def value= pair[1]
    result += "<name>$name</name>" 
    result += "<value>$value</value>" 
    result +="</comp>"
}
result +="</Item>"

Then I would use XMLSlurper to make it into an XML doc.

Something I need to note. The code above is very descriptive to make things more clear. It can be way shorter (especially with Groovy). You can also define the def variables as String

MayTheSchwartzBeWithYou
  • 1,181
  • 1
  • 16
  • 32
  • Thanks for your reply. I am beginner for groovy script. I have received compilation error as like below. `Script8.groovy: 12: unexpected token: def @ line 12, column 5. for(def kv of values) {` – Simbu Jun 03 '16 at 18:37
  • It seems groovy is not allowing me to use def inside for loop braces. – Simbu Jun 03 '16 at 18:39
  • @Simbu yeah, sorry I was just coding JS and Python for 8h and mixed everything. Try `for (kv in values)` – MayTheSchwartzBeWithYou Jun 03 '16 at 18:43
  • @Simbu @nathan-hughes solution is quite Groovy. Read more about collect entries here http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html#collectEntries(java.util.Map, groovy.lang.Closure) Think of it as similar to `map` – MayTheSchwartzBeWithYou Jun 03 '16 at 18:49