【问题标题】:JAXB substitution of complex type with primitive type用原始类型替换复杂类型的 JAXB
【发布时间】:2011-03-14 19:59:42
【问题描述】:

在我的架构中,我有一些类型只是简单地扩展了一个简单的 XSD 类型(int 或 string)。 JAXB 为每个此类类型创建一个单独的 java 类。我想删除这个中间类并配置 JAXB 以尽可能使用原语(例如,用 java.lang.StringDocumentType 替换 CountryTypeintlava.lang.Integer)。例如,对于给定的 XSD,最好有 DestinationType.setDocumentType(int)List<String> StatesType.getCountry()。我很高兴为此编写类型范围的an adapter,但看起来只支持来自原始 XML 类型的转换。也许可以进行每个属性类型的转换?请给出 JAXB 绑定自定义的任何示例,这可能会有所帮助。

<?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>

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    另一种可能性,你能改变你的架构吗?:

    以下架构更改将生成您想要的对象模型。

    用途:

    <simpleType name="documentType">
        <restriction base="integer"/>
    </simpleType>
    

    代替:

    <complexType name="documentType"> 
        <simpleContent> 
            <extension base="integer"/> 
        </simpleContent> 
    </complexType> 
    

    【讨论】:

    • 能否在不破坏序列化 XML 兼容性的情况下应用此转换?
    • @dma_k - 是的,两种类型的 XML 表示是相同的。
    【解决方案2】:

    好问题。过去,我通过 XSLT 预处理步骤运行模式来解决这个问题,“扁平化”类型层次结构,同时保留文档的语义。

    例如,XSLT 将删除 documentType 类型的定义,并将对 documentType 的每个引用替换为 integer。生成的已处理架构仍然表示相同的实例文档,但更简单,并且绑定更好。

    这个(相当半生不熟的)解决方案可以应用于许多与过于复杂的模式有关的类似问题(例如,用选择结构替换替换组)。

    【讨论】:

      猜你喜欢
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2011-02-13
      相关资源
      最近更新 更多