0

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?

Dmitry Gorshkov
  • 3,523
  • 4
  • 18
  • 20
  • BTW, your schema is mapping `meta` elements text content to `metas`. But as per xml it should be mapped to `name` attribute of meta – Santhosh Kumar Tekuri Jul 08 '15 at 16:05
  • similar question: http://stackoverflow.com/questions/25850502/jaxb-an-element-with-unbounded-maxoccurs-inside-an-xsdall – Santhosh Kumar Tekuri Jul 08 '15 at 16:11
  • @SanthoshKumarT thanks for your comments. I've corrected my class and schemas. The difference between my question and the link you sent is that I have to simple type of repeatable element. I afraid that in my case there is no possibility to validate through the schema. – Dmitry Gorshkov Jul 08 '15 at 17:47
  • why donot you try create schema for your xmlschema by hand. and generate classes using jaxb from that xmlschema. then jaxb generated classes will have correct annotations as per your requirement – Santhosh Kumar Tekuri Jul 08 '15 at 17:50
  • @SanthoshKumarT Yes it's also possible, as long as I don't need to change the xml's. But what schema do you suggest? – Dmitry Gorshkov Jul 08 '15 at 17:56
  • just give me the xml schema which matches as per your requirements – Santhosh Kumar Tekuri Jul 08 '15 at 18:02
  • @SanthoshKumarT, if I understood you right - you want several variant of correct xml. So I've added several examples to my post. – Dmitry Gorshkov Jul 08 '15 at 18:37
  • I just wanted to realise that, it is not possible to define a schema as per your given requirements. you want the order is unimportant. that means `xs:all` should be used. as per xsd specification, `xs:all` does not allow any of its member to be unbounded – Santhosh Kumar Tekuri Jul 08 '15 at 18:52
  • see http://stackoverflow.com/questions/2362365/xsd-doesnt-allow-me-to-have-unbounded-inside-all-indicator – Santhosh Kumar Tekuri Jul 08 '15 at 18:55

0 Answers0