I have an xml:
<package>
<metadata>
<title>Title</title>
<language>en</language>
<meta name="cover" />
</metadata>
</package>
Another examples of correct xml's:
<package>
<metadata>
<language>en</language>
<title>Title</title>
</metadata>
</package>
<package>
<metadata>
<meta name="content" />
<language>en</language>
<meta name="cover" />
<title>Title</title>
</metadata>
</package>
And I want to unmarshall it to the java class using JAXB library. The difficulty is that amount of "meta" element can be from 0 to multiple and order of elements "title" "language" and "meta"s can also be random.
My class looks like:
@XmlRootElement(name = "package")
@XmlAccessorType(XmlAccessType.FIELD)
public class Test {
@XmlElement(name = "metadata", required = true)
public Metadata metadata;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={})
public static class Metadata {
@XmlElement(name = "title", required = true)
public String title;
@XmlElement(name = "language", required = true)
public String language;
@XmlElement(name = "meta", required = false)
public List<Meta> metas;
}
@XmlAccessorType(XmlAccessType.FIELD)
public static class Meta {
@XmlAttribute(name = "name", required = true)
public String name;
}
}
It matches the schema:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="package" type="test"/>
<xs:complexType name="test">
<xs:sequence>
<xs:element name="metadata" type="metadata"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="metadata">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="meta" type="meta" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="meta">
<xs:sequence/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
When I unmarshall the document I get an error:
org.xml.sax.SAXParseException: cos-all-limited.2: The {max occurs} of an element in an 'all' model group must be 0 or 1. The value 'unbounded' for element 'meta' is invalid.
If I remove the attribute @XmlType(propOrder={}) it matches the schema:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="package" type="test"/>
<xs:complexType name="test">
<xs:sequence>
<xs:element name="metadata" type="metadata"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="metadata">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="meta" type="meta" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="meta">
<xs:sequence/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
And I get an error if the order of the properties is different:
Invalid content was found starting with element 'language'. One of '{title}' is expected.
I can not change the xml files, but I can modify the classes and/or annotations. I generate schema by this classes dynamically, so the schema is also can be adjust.
Any ideas how to achieve a desirable behaviour?