如何使用groovy将xsi名称空间前缀添加到xml

时间:2017-08-01 12:46:05

标签: xml groovy soapui

我尝试使用groovy(SoapUI)创建以下XML。

预期的XML

bookshelf.on('fetching', () => {
  server.app.fetching = new Date().valueOf();
});

bookshelf.on('counting', () => {
  server.app.fetching = new Date().valueOf(); 
});

bookshelf.on('fetched', () => {
  server.statsd.gauge('db_query', new Date().valueOf() - server.app.fetching);
});

使用过的代码:(没有用过)

'fetching event A'
'fetching event B'
'fetching event C'
'fetched event C'
'fetched event B'
'fetched event A'

1 个答案:

答案 0 :(得分:0)

你走了,内联评论。

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def xml = builder.bind {
    mkp.xmlDeclaration()
//Declare the namespaces
    namespaces << [ S:'http://schemas.xmlsoap.org/soap/envelope/', 
    xsi: 'http://www.w3.org/2001/XMLSchema-instance', 
    ns2: 'http://service.generic.abc.it',
    ns4: 'http://generic.abc.it']
//You can see building the xml as you wanted it
    S.Envelope {
        S.Body {
            ns2.save {
                arg1('xsi.type': 'ns4:ContractGroup' ){
                  discounts('xsi.type': 'ns4:DiscountPrice')
                }
            }            
        }        
    }
}

println XmlUtil.serialize(xml)

您可以在线快速尝试 Demo