5

There is a set of XSD files, with the following types described in them:

A is a complex type with various elements. B restricts A, keeping only some of its elements C extends B, adding some elements.

So, this means that a Java class generated by a tool using type C from the XML schema, is expected to include only those members of A which are kept by B, and the new ones added by C

To make things even more difficult, B actually changes minOccurs attribute of some of the elements.

Are there any frameworks out there which can handle this? I've tried EMF and restrictions are not even reflected to code.

tshepang
  • 12,111
  • 21
  • 91
  • 136
mahonya
  • 9,247
  • 7
  • 39
  • 68

3 Answers3

4

Use Castor CodeGenerator. It's Nice. See http://www.castor.org/reference/html-single/index.html#xml.code.generator.examples.non-trivial

Puspendu Banerjee
  • 2,631
  • 16
  • 19
2

Here's antoher alternative (for you to can choose one): The Axis2 DataBinding Framework (ADB) supports type hierarchy and custom restrictions .

Pros: It generates the full schema structure and implements getters and setters from each element. On the setters of every type y checks the schema defined restictions, and when the value to set doesn't match the restriction it throws an exception (at the very own set method), and it supports StAX for serialization/deserialization.

Cons: the generated code can result a bit too verbose, the validation exceptions are too few explicative, it's meant to be "simple" (so with complex type extensions or restrictions you could find some troubles), and it includes some dependencies with the ADB library (i.e. the generated beans implement the org.apache.axis2.databinding.ADBBean interface).

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
  • ADB page contains the following at the bottom: "Known Limitations ADB is meant to be a 'Simple' databinding framework and was not meant to compile all types of schemas. The following limitations are the most highlighted. 1. Complex Type Extensions and Restrictions. " – mahonya Nov 26 '10 at 14:49
  • Yes, I mentioned it as a Con. But I have used it for serializing some herarchichal data (a parent class, and a set of extending classes, a some composition/aggregation relations), and with the usual XSD supported restrictions (such as nullability, number of occurrences, regex matching, etc), and worked fine. The point here is that the validation exceptions were generated with an empty message, which can be kind of a problem. – Tomas Narros Nov 26 '10 at 14:54
2

When using complex XSDs I had the best results with XMLBeans.

  • It has full XML Schema support.
  • It was the only framework which could generate correct code for the intricate xml schemas I had to use.

Another really useful feature is:

When unmarshalling an XML instance the full XML infoset is kept and is available to the developer. This is critical because because of the subset of XML that is not easily represented in java. For example, order of the elements or comments might be needed in a particular application.

Martin Gross
  • 929
  • 7
  • 5