dom - Omit empty attributes with groovy DOMBuilder -


groovy's markupbuilder has omitnullattributes , omitemptyattributes. dombuilder doesn't. code have

>>> def xml = dombuilder.newinstance() >>> def maybeempty = null >>> println xml.foo(bar: maybeempty) <foo bar=""/> 

i want bar omitted if empty. found workaround in answer groovy antbuilder, omit conditional attributes... findall empty attributes , remove them. since have complex dom generated, i'm looking other options.

i believe there no built-in option that, if need dombuilder, subclass , filter attributes...

@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}      } } 

you might want tune construction in standard dombuilder, example.

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"/>     

standard output said was...

println groovy.xml.dombuilder.newinstance().foo(bar: null, baz: 1) //<?xml version="1.0" encoding="utf-8"?> //<foo bar="" baz="1"/> 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -