我刚刚和我的同事发现了一个奇怪的 JAXB 案例。
我声明了一个@XmlElement(required = true)
默认nillable = false
字段。
但是我偶然地试图编组一个值为 null 的实例。
我看到该元素只是被省略了。正常吗?这只是程序员的错吗?
import java.io.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class Doughan {
public static void main(final String[] args) throws JAXBException, IOException {
final JAXBContext context =
JAXBContext.newInstance(Doughan.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
System.out.println("\n------------------------------------- not valid, is it?");
marshaller.marshal(new Doughan(), System.out);
System.out.println("\n------------------------------------------------- valid");
marshaller.marshal(new Doughan("Blaise"), System.out);
System.out.println("\n------------------------------------------------ schema");
context.generateSchema(new SchemaOutputResolver(){
@Override public Result createOutput(final String namespaceUri,
final String suggestedFileName)
throws IOException {
return new StreamResult(System.out) {
@Override public String getSystemId() {
return "Do I really have to do this?";
}
};
}
});
}
public Doughan() {
this(null);
}
public Doughan(final String name) {
super();
this.name = name;
}
@XmlElement(required = true) // default; nillable = false
private String name;
@XmlElement(nillable = true, required = true)
private String nill = null;
}
我得到的东西来了。
------------------------------------- not valid, is it?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
-------------------------------------- valid, isn't it?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<name>Blaise</name>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
------------------------------------------------ schema
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doughan" type="doughan"/>
<xs:complexType name="doughan">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="nill" type="xs:string" nillable="true"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
xmllint
$ head -n100 doughan.xsd doughan1.xml doughan2.xml
==> doughan.xsd <==
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doughan" type="doughan"/>
<xs:complexType name="doughan">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="nill" type="xs:string" nillable="true"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
==> doughan1.xml <==
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
==> doughan2.xml <==
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<name>Blaise</name>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
$ xmllint --schema doughan.xsd doughan1.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
Element 'nill': This element is not expected. Expected is ( name ).
doughan1.xml fails to validate
$ xmllint --schema doughan.xsd doughan2.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doughan>
<name>Blaise</name>
<nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</doughan>
doughan2.xml validates
$
带着敬意。