【问题标题】:Groovy++ and XMLGroovy++ 和 XML
【发布时间】:2011-06-14 10:07:30
【问题描述】:

可以使用静态类型的 Groovy++ 进行 XML 处理吗?

示例(您可以使用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
​

结果:

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

1 error

【问题讨论】:

    标签: xml groovy groovy++


    【解决方案1】:

    看起来不像……你可以使用:

    @Typed(TypePolicy.MIXED)
    

    然后

    println records.car
    

    有效,但您无法通过以下方式获取属性值:

    println records.car.@name
    

    当你得到错误:

    Cannot find field name of class Object
    

    我猜 XmlSlurping 目前对于 groovy++ 来说有点过于动态了

    【讨论】:

    • 我无法让@Typed(TypePolicy.MIXED) 工作(未解析的符号),但删除@Typed package test 一起解决了问题
    • @ronk - 删除 @Typed package test 你用 groovy 而不是 groovy++ 编译它
    猜你喜欢
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多