有什么方法可以让 JAXB 正确打印xmlns:xsi
和xsi:nill
nillable @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;
}
印刷
<?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>
我只是想知道有没有什么办法可以让第一个<value/>
武装xmlns:xsi
起来xsi:nill
。