DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • How to Convert XLS to XLSX in Java
  • Automatic Versioning in Mobile Apps
  • Handling Dynamic Data Using Schema Evolution in Delta

Trending

  • Metrics at a Glance for Production Clusters
  • Data Quality: A Novel Perspective for 2025
  • How to Perform Custom Error Handling With ANTLR
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Coding
  3. Languages
  4. Reusing Generated JAXB Classes

Reusing Generated JAXB Classes

By 
Blaise Doughan user avatar
Blaise Doughan
·
Dec. 06, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
15.8K Views

Join the DZone community and get the full member experience.

Join For Free
In this post I will demonstrate how to leverage the -episode XJC extension to reuse classes previously generated from.an XML schema.  This is useful when an XML schema is imported by other XML schemas and you do not want the same classes generated each time.

Imported Schema (Product.xsd)

The following XML schema represents basic information about a product.  Product is a common concept in this example domain so I have decided to define one representation that can be leveraged by other schemas, rather than having each schema define its own representation of product information.
<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/Product"
    xmlns:tns="http://www.example.org/Product"
    elementFormDefault="qualified">
    <element name="product">
        <complexType>
            <sequence>
                <element name="id" type="string"/>
                <element name="name" type="string"/>
            </sequence>
        </complexType>
    </element>
</schema>
Since multiple XML schemas import Product.xsd we can leverage episode files so that the classes corresponding to Product.xsd are only generated once.  The following XJC call demonstrates how to generate an episode file called product.episode along with the generated classes:
xjc -d out -episode product.episode Product.xsd
Importing Schema (ProductPurchaseRequest.xsd)

Below is an example of an XML schema that imports Product.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/ProductPurchaseRequest"
    xmlns:tns="http://www.example.org/ProductPurchaseRequest"
    xmlns:prod="http://www.example.org/Product"
    elementFormDefault="qualified">
    <import namespace="http://www.example.org/Product" schemaLocation="Product.xsd"/>
    <element name="purchase-request">
        <complexType>
            <sequence>
                <element ref="prod:product" maxOccurs="unbounded"/>
            </sequence>
        </complexType>
    </element>
</schema>
When we generate classes from this XML schema we will reference the episode file we created when we generated Java classes from Product.xsd.  If we do not specify the episode file then classes will be generated for both ProductPurchaseRequest.xsd and Product.xsd:
xjc -d out ProductPurchaseRequest.xsd -extension -b product.episode
Another Importing Schema (ProductQuoteRequest.xsd)

Below is another example of an XML schema that imports Product.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/ProductQuoteRequest"
    xmlns:tns="http://www.example.org/ProductQuoteRequest"
    xmlns:prod="http://www.example.org/Product"
    elementFormDefault="qualified">
    <import namespace="http://www.example.org/Product" schemaLocation="Product.xsd"/>
    <element name="quote">
        <complexType>
            <sequence>
                <element ref="prod:product"/>
            </sequence>
        </complexType>
    </element>
</schema>


Again when we generate classes from this XML schema we will reference the episode file we created when we generated Java classes from Product.xsd.

xjc -d out ProductQuoteRequest.xsd -extension -b product.episode
How Does it Work? (product.episode)

For those of you curious how this works.  The episode file generated by XJC is really just a standard JAXB bindings file that is used to customize the class generation.  This generated bindings/episode file contains entries that tells XJC that a class already exists for this type.  You could write this file by hand, but -episode flag to XJC does it for you.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
  <!--
 
This file was generated by the JavaTM Architecture for XML Binding
(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 See
<a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
Any modifications to this file will be lost upon recompilation of the
source schema.
Generated on: 2011.11.02 at 03:40:10 PM EDT
 
  -->
  <bindings scd="x-schema::tns"
    xmlns:tns="http://www.example.org/Product">
    <schemaBindings map="false"/>
    <bindings scd="tns:product">
      <class ref="org.example.product.Product"/>
    </bindings>
  </bindings>
</bindings>

 

 

From http://blog.bdoughan.com/2011/12/reusing-generated-jaxb-classes.html

Schema XML

Opinions expressed by DZone contributors are their own.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • How to Convert XLS to XLSX in Java
  • Automatic Versioning in Mobile Apps
  • Handling Dynamic Data Using Schema Evolution in Delta

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!