2
有沒有什麼辦法讓JAXB正確打印xmlns:xsi
和xsi:nill
在nillable上@XmlRootElement
?`@ XmlRootElement`和`nillable`
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>
我只是想知道有沒有什麼辦法讓與xmlns:xsi
和xsi:nill
武裝第一<value/>
。
親愛的布萊斯Doughan先生。你可以在'@ XmlRootElement'上添加'nillable'屬性嗎? – 2012-08-03 13:34:28
這是一個有趣的請求。可以將此請求通過電子郵件發送給users @ jaxb.java.net'? – 2012-08-03 14:26:04