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

1

8

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.

Suited answered 2/11, 2011 at 16:33 Comment(0)
S
1

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).
Sarson answered 28/7, 2018 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.