简体   繁体   中英

Is it possible to have JAXB ignore order of elements in a <sequence> when one or more elements is a list (maxOccurs=unbounded)?

I have been unable to find an answer to this. The <all> tag is not the answer, as it restricts child elements to maxOccurs=“1” .

Say you had XSD that included:

<xsd:sequence>
  <xsd:element name=“name” type=“xsd:string” minOccurs=“1”/>
  <xsd:element name=“children” type=“xsd:string” minOccurs=“0” maxOccurs=“unbounded”/>
</xsd:sequence>

Is there any way to get JAXB to use this XSD to read XML where the ordering of elements is not the same as defined in the XSD?

I am not aware of a full solution with JAXB.

However, the JAXB binding for

<xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="children" type="xs:string"/> <xs:element name="name" type="xs:string"/> </xs:choice> </xs:complexType>

will unmarshall the instances that you want and keeps them stable when marshalling them again.

Downsides are:

  • The corresponding Java class has a (typed) List childrenOrName, not two individual properties (in which case you would loose the position of your name element anyway)
  • You have to check manually that there is exactly one name in your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM