users@jaxb.java.net

nillable for _at_XmlRootElement

From: Jin Kwon <onacit_at_mac.com>
Date: Fri, 03 Aug 2012 23:57:13 +0900

This is a question being asked to 'users_at_jaxb.java.net'.

Is there any way to let JAXB properly prints|xmlns:xsi|and|xsi:nill|on
nillable|_at_XmlRootElement|?

|public class XmlValueTest {

     public static void main(final String[] args) throws JAXBException {

         final JAXBContext context=
             JAXBContext.newInstance(Wrapper.class, Value.class);

         final Marshaller marshaller= context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

         marshaller.marshal(Value.newInstance(null), System.out);
         marshaller.marshal(Value.newInstance("null"), System.out);
         marshaller.marshal(Wrapper.newInstance(null), System.out);
         marshaller.marshal(Wrapper.newInstance("null"), System.out);
     }
}

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
class Value {

     public static Value newInstance(final String raw) {
         final Value instance= new Value();
         instance.raw= raw;
         return instance;
     }

     @XmlValue
     private String raw;
}

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
class Wrapper {

     public static Wrapper newInstance(final String raw) {
         final Wrapper wrapper= new Wrapper();
         wrapper.raw= raw;
         return wrapper;
     }

     @XmlElement(nillable= true, required= true)
     private String raw;
}
|

prints

|<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<value/> <!-- is this normal? -->

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<value>null</value>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wrapper>
     <raw xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</wrapper>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wrapper>
     <raw>null</raw>
</wrapper>|