JAXB 2 (Oracle / Metro バージョン 2.2.7 と私は他のものも同様だと思います) は、列挙要素の値の周りの空白を許容していないようです。
最小限の例を次に示します。xmllintとXercesの両方が、スキーマに対してインスタンスを検証します。不可解な考えは、値にアクセスしようとすると、JAXB 検証は文句を言わずにnullを返すということです。値を正しく返すように構成するにはどうすればよいですか?
更新:ここXmlAdapter
で提案されているように、文字列をトリミングするために を関連付けてみましたが、結果は同じです。
更新 II:そして、これが Metro JAXB Jira のチケットです。
A.xsd
<xs:schema targetNamespace="foo://a"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="foo://a">
<xs:element name="type" type="Type"/>
<xs:simpleType name="Type">
<xs:restriction base="xs:token">
<xs:enumeration value="Archive"/>
<xs:enumeration value="Organisation"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
a.xml
<a:type xmlns:a="foo://a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="foo://a A.xsd"
>Organisation </a:type>
(「組織」の後の空白に注意してください)
コードのアンマーシャリング
public static void main(String args[]) throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
JAXBContext payloadContext = JAXBContext.newInstance("a");
Unmarshaller unmarshaller = payloadContext.createUnmarshaller();
unmarshaller.setSchema(schemaFactory.newSchema(new Source[]{new StreamSource(new FileInputStream(new File("A.xsd")))}));
JAXBElement<?> oUnmarshalled = (JAXBElement<?>) unmarshaller.unmarshal(new File("a.xml"));
Object o = oUnmarshalled.getValue(); // returns NULL
}