Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB: generate Java classes only for a subset of the XSD types

Tags:

java

jaxb

jaxb2

I have a couple of huge XML schema definition (XSD) files and I want to generate only for a subset of the defined types the corresponding Java classes.

More precisely I have a list of "root" types that I want to transform into Java classes including all types needed by these root types.

Is it possible to define some "root" types in a JAXB bindings file and tell JAXB to transform only them with all their dependent types into Java classes and ignore all the other unnecessary types?

Thanks in advance.

like image 567
Joern Avatar asked Nov 02 '11 16:11

Joern


1 Answers

There may be a more straightforward way, but one way is to make copies of the XSDs and remove all XML types from the copies excepted the root types you want with their dependencies. Then apply xjc on the copies instead of the original ones.

You can automate this process with XSLT and build automation tools like Maven, Gradle, Ant, etc. You first write the XSLT stylesheet that transforms the XSDs to copy only the root types with dependencies, and save the result to a temporary location (e.g. target/generated-sources folder with Maven). Then, with Maven for example, you automate the process with build plugins in your pom.xml:

  1. Run the XSLT transformation with Maven XML plugin, using preferably Saxon as XSLT processor.
  2. Run JAXB2 Maven plugin to generate the Java classes from the new XSD copies (with schemaDirectory/schemaIncludes parameters).
like image 130
cdan Avatar answered Nov 09 '22 08:11

cdan