Groovy++ and XML

163 Views Asked by At

Can be used statically typed Groovy++ for XML processing?

Example (for testing you can use Groovy++ web console):

@Typed package test
def CAR_RECORDS = '''
  <records>
    <car name='HSV Maloo' make='Holden' year='2006'>
      <country>Australia</country>
      <record type='speed'>Production Pickup Truck with speed of 271kph</record>
    </car>
  </records>
'''

def records = new XmlSlurper().parseText(CAR_RECORDS)
println records.car
​

The result:

startup failed:
Script1.groovy: 14: Cannot find property car of class GPathResult
 @ line 14, column 9.
   records.car
           ^

1 error
1

There are 1 best solutions below

2
On

Doesn't look like it... You can use:

@Typed(TypePolicy.MIXED)

and then

println records.car

works, but you cannot get to attribute values via:

println records.car.@name

As you then get the error:

Cannot find field name of class Object

I guess XmlSlurping is currently a bit too dynamic for groovy++