在我的模式中,我有一些类型只是扩展了一个简单的 XSD 类型(int 或 string)。 JAXB 为每个此类类型创建一个单独的 java 类。我想删除这个中间类并将 JAXB 配置为尽可能使用原语(例如,将 CountryType
替换为 java.lang.String
并将 DocumentType
替换为 int
或 lava.lang.Integer
)。例如,对于给定的 XSD,最好有 DestinationType.setDocumentType(int)
和 List<String> StatesType.getCountry()
.我很高兴写全类型an adapter为此,但看起来只支持原始 XML 类型的转换。也许可以进行每个属性的类型转换?请给出 JAXB 绑定(bind)自定义的任何示例,这可以提供帮助。
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:exch="http://www.mycompany.org/exchange"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="http://www.mycompany.org/exchange"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<complexType name="countryType">
<simpleContent>
<extension base="string"/>
</simpleContent>
</complexType>
<complexType name="statesType">
<sequence maxOccurs="unbounded">
<element name="country" type="exch:countryType"/>
</sequence>
</complexType>
<complexType name="documentType">
<simpleContent>
<extension base="integer"/>
</simpleContent>
</complexType>
<complexType name="destinationType">
<sequence>
<element name="states" type="exch:statesType" maxOccurs="1"/>
<element name="document-type" type="exch:documentType" minOccurs="1" maxOccurs="1"/>
</sequence>
</complexType>
</schema>
最佳答案
另一种可能性,你能改变你的模式吗?:
以下架构更改将生成您想要的对象模型。
使用:
<simpleType name="documentType">
<restriction base="integer"/>
</simpleType>
代替:
<complexType name="documentType">
<simpleContent>
<extension base="integer"/>
</simpleContent>
</complexType>
关于java - JAXB 将复杂类型替换为原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3229093/