java - JAXB - Suppress Boolean attribute if false -
let's have class
@xmlrootelement(name="thing") public class thing{ private string name; private boolean awesome; @xmlvalue public void setname(string name) { this.name = name; } public string getname() { return this.value; } @xmlattribute public void setawesome(boolean awesome) { this.awesome = awesome; } public boolean isawesome() { return this.awesome; } }
if create things, marshall them xml, looks this:
a flying ninja:
<thing awesome="true">flying ninja</thing>
a regular old popcorn ball:
<thing awesome="false">popcorn ball</thing>
but... change way boolean attributes marshalled. rather see popcorn ball this, supressing awesome attribute:
<thing>popcorn ball</thing>
how can this?
thanks much!
note: i'm eclipselink jaxb (moxy) lead, , member of jaxb 2.x (jsr-222) expert group.
using moxy can xmladapter
approach suggested hovercraft full of eels. like:
booleanadapter
import javax.xml.bind.annotation.adapters.xmladapter; public class booleanadapter extends xmladapter<boolean, boolean> { @override public boolean unmarshal(boolean v) throws exception { return boolean.true.equals(v); } @override public boolean marshal(boolean v) throws exception { if(v) { return v; } return null; } }
thing
you associate property xmladapter
using @xmljavatypeadapter
annotation follows:
import javax.xml.bind.annotation.xmlattribute; import javax.xml.bind.annotation.xmlrootelement; import javax.xml.bind.annotation.xmlvalue; import javax.xml.bind.annotation.adapters.xmljavatypeadapter; @xmlrootelement(name="thing") public class thing{ private string name; private boolean awesome; @xmlvalue public void setname(string name) { this.name = name; } public string getname() { return this.name; } @xmlattribute @xmljavatypeadapter(booleanadapter.class) public void setawesome(boolean awesome) { this.awesome = awesome; } public boolean isawesome() { return this.awesome; } }
demo
import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(thing.class); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); thing thing = new thing(); thing.setname("popcorn ball"); thing.setawesome(false); marshaller.marshal(thing, system.out); thing.setawesome(true); marshaller.marshal(thing, system.out); } }
output
<?xml version="1.0" encoding="utf-8"?> <thing>popcorn ball</thing> <?xml version="1.0" encoding="utf-8"?> <thing awesome="true">popcorn ball</thing>
using jaxb ri
if run example jaxb ri following exception:
exception in thread "main" com.sun.xml.bind.v2.runtime.illegalannotationsexception: 1 counts of illegalannotationexceptions adapter example.booleanadapter not applicable field type boolean. problem related following location: @ @javax.xml.bind.annotation.adapters.xmljavatypeadapter(type=class javax.xml.bind.annotation.adapters.xmljavatypeadapter$default, value=class example.booleanadapter) @ public boolean example.thing.isawesome() @ forum251.thing
for more information
Comments
Post a Comment