我相信没有内置选项,但如果你需要一个 DOMBuilder,你可以子类化它并过滤属性......
@groovy.transform.InheritConstructors
class DOMBuilderSubclass extends groovy.xml.DOMBuilder {
@Override
protected Object createNode(Object name, Map attributes) {
super.createNode name, attributes.findAll{it.value != null}
}
}
您可能希望像在标准 DOMBuilder 中一样调整构造,这只是一个示例。
def factory = groovy.xml.FactorySupport.createDocumentBuilderFactory().newDocumentBuilder()
def builder = new DOMBuilderSubclass(factory)
println builder.foo(bar: null, baz: 1)
//<?xml version="1.0" encoding="UTF-8"?>
//<foo baz="1"/>
正如你所说的标准输出是......
println groovy.xml.DOMBuilder.newInstance().foo(bar: null, baz: 1)
//<?xml version="1.0" encoding="UTF-8"?>
//<foo bar="" baz="1"/>