Reusing Generated JAXB Classes
Join the DZone community and get the full member experience.
Join For FreeThe 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>
xjc -d out -episode product.episode 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>
xjc -d out ProductPurchaseRequest.xsd -extension -b product.episode
<?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>
xjc -d out ProductQuoteRequest.xsd -extension -b product.episode
<?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
Opinions expressed by DZone contributors are their own.
Comments