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类结果如下
[quote]
public class PhysicalAddress {

protected AddressLine1 addressLine1;
protected AddressLine2 addressLine2;
protected AddressLine3 addressLine3;
protected CityName cityName;
protected String globalCountryCode;
protected String globalLocationIdentifier;
protected String nationalPostalCode;
protected PostOfficeBoxIdentifier postOfficeBoxIdentifier;
protected RegionName regionName;
}
[/quote]

但是,如下的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)的注释暂时不用管
[quote]
public class PartnerBusinessIdentification {

@XmlElementRefs({
@XmlElementRef(name = "ProprietaryBusinessIdentifier", type = JAXBElement.class),
@XmlElementRef(name = "ProprietaryDomainIdentifier", type = JAXBElement.class),
@XmlElementRef(name = "ProprietaryIdentifierAuthority", type = JAXBElement.class)
})

protected List<JAXBElement<String>> proprietaryBusinessIdentifierAndProprietaryDomainIdentifierAndProprietaryIdentifierAuthority;
}
[/quote]
我就不明白为什么不是生成以下这样的类结构呢?
[quote]
public class PartnerBusinessIdentification {
protected ProprietaryBusinessIdentifier proprietaryBusinessIdentifier;
protected ProprietaryDomainIdentifier proprietaryDomainIdentifier;
protected ProprietaryIdentifierAuthority proprietaryIdentifierAuthority;
}
[/quote]

后来比较多个类似结构的schema发现,问题应该是在<xs:sequence>上,[b]如果它加上了maxOccurs="unbounded"属性的话,就只会在此元素对应的java类中生成一个List<?>类型的元素,而不管里面<xs:sequence>究竟有一个还是多个子元素[/b],这样一来生成的java类就非常难处理了。我正在想对策中
发布了3 篇原创文章 · 获赞 0 · 访问量 815
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章