【问题标题】:Groovy xml parsingGroovy xml 解析
【发布时间】:2011-02-03 20:30:38
【问题描述】:

您好,我是 groovy 新手,我对 groovy 如何解析 xml 有一个误解

<schema-definition name="Standard" > 
  <field-type name="Standard:Integer" descr="A whole number." base-type="long"/> 
  <field-type name="Standard:Boolean" descr="A boolean value." base-type="boolean"/> 
  <field-type name="Standard:String" descr="A string" base-type="string"/> 
</schema-definition>

当我尝试时

document = groovy.xml.DOMBuilder.parse(new FileReader('D:/test.xml'))
rootElement = document.documentElement
use(groovy.xml.dom.DOMCategory)
{
    field-types=rootElement.field-type
    println field-types
}

我什至不编译: (field - types) 是二进制表达式,但它应该是第 13 行第 20 行的变量表达式

【问题讨论】:

  • 这看起来很麻烦。为什么不试试 XMLSlurper。它更加 Groovy!

标签: java xml groovy


【解决方案1】:

它无法编译,因为字段类型中的“-”是一个运算符,因此不能作为名称的一部分。此外,如果您访问标签字段类型,则必须使用引号,如下所示:

def fieldTypes = rootElement."field-type"

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多