2

我尝试使用创建 XML

def builder = new groovy.xml.StreamingMarkupBuilder()
builder.encoding = "UTF-8"
def person = {
    mkp.xmlDeclaration()
    //mkp.declareNamespace('location':'http://someOtherNamespace')
    person(id:100){
        firstname("Jane")
        lastname("Doe")
        location.address("123 Main")
    }
}
println builder.bind(person)

我收到了这个错误:

Caught: groovy.lang.GroovyRuntimeException: Namespace prefix: location is not bound to a URI
groovy.lang.GroovyRuntimeException: Namespace prefix: location is not bound to a URI
    at MyTest$_run_closure1_closure2.doCall(MyTest.groovy:9)
    at MyTest$_run_closure1.doCall(MyTest.groovy:6)
    at MyTest.run(MyTest.groovy:12)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我不需要命名空间。我只需要声明

4

1 回答 1

4

取决于您希望您的 XML 看起来像什么(您没有在问题中说):

如果你想:

<location><address>123 Main</address></location>

然后location.address("123 Main")改为:

   location {
      address("123 Main")
   }

如果你想:

<location address='123 Main'/>

然后将其更改为:

   location( address:"123 Main" )
于 2013-07-26T12:28:03.230 回答