如何使用Groovy';s MarkupTemplateEngine

如何使用Groovy';s MarkupTemplateEngine,groovy,Groovy,有没有一种方法可以使用MarkupTemplateEngine使标记具有名称空间 我希望能够做到以下几点: sp.setNameSpaceDeclaration(“”) 得到 <?xml version='1.0' encoding='UTF-8'?> <cars xmlns:sp="http://example.org/myns"> <car make='Peugeot' model='508'/>\ <sp:brand attribu

有没有一种方法可以使用MarkupTemplateEngine使标记具有名称空间

我希望能够做到以下几点: sp.setNameSpaceDeclaration(“”)

得到

<?xml version='1.0' encoding='UTF-8'?>
<cars xmlns:sp="http://example.org/myns">
    <car make='Peugeot' model='508'/>\
    <sp:brand attribute='year'>my text</sp:brand>
    <car make='Toyota' model='Prius'/>
    <sp:brand attribute='year'>my text</sp:brand>
</cars>

你快到了。为了完整起见,我已经包含了StreamingMarkupBuilder初始化

def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'

def cars = builder.bind {
    mkp.xmlDeclaration()
    namespaces << [sp:'http://example.org/myns']
    cars('xmlns:sp':"http://example.org/myns") {
       cars.each {
          car(make: it.make, model: it.model)
         'sp:brand'(attribute:'year', 'my text')
       }
    }     
}
def builder=new StreamingMarkupBuilder()
builder.encoding='UTF-8'
def cars=builder.bind{
mkp.xml声明()

名称空间我想使用一个模板文件,所以我现在处理这个问题的方式是:

汽车.第三方物流模板:

 xmlDeclaration()
 cars('xmlns:sp':"http://www.w3schools.com/furniture") {
   comment commentTest
   newLine()
   cars.each {
      car(make: it.make, model: it.model)
      newLine()
      'sp:brand'(attribute:'year', 'my text')
      newLine()
   }
}
节目

    URL url = this.getClass().getResource("/cars.tpl");
    File markupTemplate = new File(url.getFile());

    TemplateConfiguration config = new TemplateConfiguration();
    config.setAutoNewLine(true);
    config.setAutoIndent(true);
    config.setDeclarationEncoding('UTF-8');
    MarkupTemplateEngine engine = new MarkupTemplateEngine(config);

    Template template = engine.createTemplate(markupTemplate);
    def model = [commentTest: 'alberto', cars: [new Car(make: 'Peugeot', model: '508'), new Car(make: 'Toyota', model: 'Prius')]]

    def fout = new File("C:\\code\\cesa\\src\\test\\resources\\cars.xml")
    fout.withWriter('UTF-8') {writer ->
        template.make(model).writeTo(writer)
    }

谢谢sbglasius!不过我确实想使用MarkupTemplateEngine,因为它允许将模板作为单独的模板并包含嵌套模板。尽管您的解决方案运行良好,但我会将我正在做的作为答案发布。
 xmlDeclaration()
 cars('xmlns:sp':"http://www.w3schools.com/furniture") {
   comment commentTest
   newLine()
   cars.each {
      car(make: it.make, model: it.model)
      newLine()
      'sp:brand'(attribute:'year', 'my text')
      newLine()
   }
}
    URL url = this.getClass().getResource("/cars.tpl");
    File markupTemplate = new File(url.getFile());

    TemplateConfiguration config = new TemplateConfiguration();
    config.setAutoNewLine(true);
    config.setAutoIndent(true);
    config.setDeclarationEncoding('UTF-8');
    MarkupTemplateEngine engine = new MarkupTemplateEngine(config);

    Template template = engine.createTemplate(markupTemplate);
    def model = [commentTest: 'alberto', cars: [new Car(make: 'Peugeot', model: '508'), new Car(make: 'Toyota', model: 'Prius')]]

    def fout = new File("C:\\code\\cesa\\src\\test\\resources\\cars.xml")
    fout.withWriter('UTF-8') {writer ->
        template.make(model).writeTo(writer)
    }