【问题标题】:JiBX generates binding that ignores attribute namespaceJiBX 生成忽略属性命名空间的绑定
【发布时间】:2015-10-26 17:03:01
【问题描述】:

我有以下架构(片段):

<xs:complexType name="partnerPaymentsItemType">
    <xs:sequence>
        <xs:element name="changeTime" type="dateTime"/>
        <xs:element name="statusId" type="shortId"/>
        <xs:element name="paymentPointId" type="shortString"/>
        <xs:element name="money" type="currency"/>
        <xs:element name="paymentDestination" type="shortString"/>
        <xs:element name="paymentDestinationType" type="shortId"/>
        <xs:element name="subagentId" type="shortId" minOccurs="0"/>
        <xs:element name="discountCardNumber" type="xs:string" minOccurs="0"/>
        <xs:element name="amountAll" type="currency" minOccurs="0"/>
        <xs:element name="rewardPercent" type="percentAmount" minOccurs="0"/>
        <xs:element name="rewardPercentValue" type="percentAmount" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="paymentTime" use="required" type="dateTime"/>
    <xs:attribute name="externalId" use="required" type="id"/>
    <xs:attribute name="registeredId" use="required" type="id"/>
</xs:complexType>

我使用 JibX Codegen 工具从中生成源代码,然后编译绑定,这应该允许我将 XML 解组为 Java 对象。这是我的代码生成设置:

<schema-set xmlns:xs="http://www.w3.org/2001/XMLSchema"
        delete-annotations="true"
        prefer-inline="false"
        generate-all="true"
        show-schema="true"
        type-substitutions="xs:date xs:string"
        package="here.lays.my.package">
    <class-decorator     class="org.jibx.schema.codegen.extend.SerializableDecorator"/>
</schema-set>

稍后,我尝试解析一个 XML 文档,该文档具有标签和属性的命名空间前缀,这导致异常提示

缺少必需的属性“paymentTime”

通过 JiBX 源的调试表明,它试图在文档中查找没有命名空间和名称“paymentTime”的属性,而文档具有映射到 URL 的命名空间的属性,当然无法找到它。

我已经反编译了与 JAD 的绑定,它会搜索具有空命名空间的属性:

public static PartnerPaymentsItemType JiBX_beeline_binding_unmarshalAttr_1_93(PartnerPaymentsItemType arg1, UnmarshallingContext arg2)
    throws JiBXException
{
    arg2.pushTrackedObject(arg1);
    arg1;
    arg1.setPaymentTime(arg2.attributeText(null, "paymentTime"));
    arg1.setExternalId(Utility.parseLong(WhitespaceConversions.trim(arg2.attributeText(null, "externalId"))));
    arg1.setRegisteredId(Utility.parseLong(WhitespaceConversions.trim(arg2.attributeText(null, "registeredId"))));
    arg2.popObject();
    return arg1;
}

如果有任何可以帮助解决问题的建议,我将不胜感激 - 例如,为什么 JiBX 会生成这样的映射,如何使其尊重属性命名空间等等。

【问题讨论】:

    标签: java xsd code-generation jibx


    【解决方案1】:

    JiBX 行为实际上是正确的,属性的命名空间前缀不是基于 xsd。

    实际的解决方案是用 XJC/JAXB 替换 JiBX,并为我需要属性名称空间前缀的实体单独架构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2018-10-19
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      相关资源
      最近更新 更多