JAXB XJC解析机制

大家都知道使用JAXB的xjc工具可以把schema自动生成对应的java类,这仔细对比了一下不同schema的结构生成的java类,有一些不明白的地方

如果schema定义如下

<xs:element name="PhysicalAddress">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" ref="addressLine1" />
        <xs:element minOccurs="0" maxOccurs="1" ref="addressLine2" />
        <xs:element minOccurs="0" maxOccurs="1" ref="addressLine3" />
        <xs:element minOccurs="0" maxOccurs="1" ref="cityName" />
        <xs:element minOccurs="0" maxOccurs="1" ref="GlobalCountryCode" />
        <xs:element minOccurs="0" maxOccurs="1" ref="GlobalLocationIdentifier" />
        <xs:element minOccurs="0" maxOccurs="1" ref="NationalPostalCode" />
        <xs:element minOccurs="0" maxOccurs="1" ref="postOfficeBoxIdentifier" />
        <xs:element minOccurs="0" maxOccurs="1" ref="regionName" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

生成的java类结果如下

引用

publicclassPhysicalAddress{

protectedAddressLine1addressLine1;

protectedAddressLine2addressLine2;

protectedAddressLine3addressLine3;

protectedCityNamecityName;

protectedStringglobalCountryCode;

protectedStringglobalLocationIdentifier;

protectedStringnationalPostalCode;

protectedPostOfficeBoxIdentifierpostOfficeBoxIdentifier;

protectedRegionNameregionName;

}

但是,如下的schema结构

<xs:element name="PartnerBusinessIdentification">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="ProprietaryBusinessIdentifier" />
        <xs:element ref="ProprietaryDomainIdentifier" />
        <xs:element minOccurs="0" maxOccurs="1" ref="ProprietaryIdentifierAuthority" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

生成的java类却如下所示,可以忽略@XMLElementRef(s)的注释暂时不用管

引用

publicclassPartnerBusinessIdentification{

@XmlElementRefs({

@XmlElementRef(name="ProprietaryBusinessIdentifier",type=JAXBElement.class),

@XmlElementRef(name="ProprietaryDomainIdentifier",type=JAXBElement.class),

@XmlElementRef(name="ProprietaryIdentifierAuthority",type=JAXBElement.class)

})

protectedList<JAXBElement<String>>proprietaryBusinessIdentifierAndProprietaryDomainIdentifierAndProprietaryIdentifierAuthority;

}

我就不明白为什么不是生成以下这样的类结构呢?

引用

publicclassPartnerBusinessIdentification{

protectedProprietaryBusinessIdentifierproprietaryBusinessIdentifier;

protectedProprietaryDomainIdentifierproprietaryDomainIdentifier;

protectedProprietaryIdentifierAuthorityproprietaryIdentifierAuthority;

}

后来比较多个类似结构的schema发现,问题应该是在<xs:sequence>上,如果它加上了maxOccurs="unbounded"属性的话,就只会在此元素对应的java类中生成一个List<?>类型的元素,而不管里面<xs:sequence>究竟有一个还是多个子元素,这样一来生成的java类就非常难处理了。我正在想对策中

相关推荐