JAXB序列化数组名称错误

时间:2016-08-16 19:23:47

标签: java xml xsd jaxb jaxb2

我有一个像这样的xsd架构定义(定义是提供给我的,并由wsdl使用,所以我试图避免更改架构):

...
<xs:complexType name="PricingServicesGroupOwner">
    <xs:sequence>
        ...
        <xs:element minOccurs="0" name="servicePreferences" nillable="true"
                    type="tns:ArrayOfServicePreferenceOwner"/>
        ...
    </xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfServicePreferenceOwner">
    <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="ServicePreferenceOwner" nillable="true"
                    type="tns:ServicePreferenceOwner"/>
    </xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfServicePreferenceOwner" nillable="true" type="tns:ArrayOfServicePreferenceOwner"/>
...

问题出在类型&#34; ArrayOfServicePreferenceOwner&#34;的内部元素中。应该命名为&#34; servicePreferences&#34;所以我希望在序列化的xml元素中看到该名称,但是当对象被序列化时,我得到了这个:

...
    <ns3:PricingServicesGroupOwner>
        ...
        <ns3:ArrayOfServicePreferenceOwner>
            <ns4:ServicePreferenceOwner>
            ...
        </ns3:ArrayOfServicePreferenceOwner>
            ...
    </ns3:PricingServicesGroupOwner>
...

预计是这样的:

...
    <ns3:PricingServicesGroupOwner>
        ...
        <ns3:servicePreferences>
            <ns4:ServicePreferenceOwner>
            ...
        </ns3:servicePreferences>
            ...
    </ns3:PricingServicesGroupOwner>
...

JAXB类具有带注释的名称定义:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfServicePreferenceOwner", namespace = "http://myschema...", ...
public class ArrayOfServicePreferenceOwner {

我尝试了什么:

  1. 更改注释中的name属性:

    @XmlType(name ="servicePreferences",...
    
  2. 使用jaxb绑定:

    <jaxb:bindings schemaLocation="serviceSchema.xsd">
        <jaxb:bindings node="//xs:element[@name='ArrayOfServicePreferenceOwner']">
            <jaxb:property name="servicePreferences"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:complexType[@name='ArrayOfServicePreferenceOwner']">
            <jaxb:property name="servicePreferences"/>
        </jaxb:bindings>
    </jaxb:bindings>
    
  3. 这些都没有效果。

1 个答案:

答案 0 :(得分:0)

在PricingServicesGroupOwner类中,对元素ArrayOfServicePreferenceOwner使用@XmlElement(name =&#34; servicePreferences&#34;)。