同僚と 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
$
敬意を表して。