2013-07-26 177 views
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) 

我得到这个错误创建XML:

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

取决于你想要什么样的XML,以便看起来像(你不要在你的问题):

如果你想:

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

然后换location.address("123 Main")到:

location { 
     address("123 Main") 
    } 

如果你想:

<location address='123 Main'/> 

然后将其更改为:

location(address:"123 Main") 
+0

然后纪念他的答案回复:http://blog.stackoverflow.com/2011/01/how-to-say-thanks-in-an-answer/ –