2013-07-26 144 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/ –