JAXB xjc for existing domain objects

I did a lot of searching and can't find a brief example of how to map an XML schema to existing domain objects, rather than create new ones using xjc. I created a bindings file (xjb), but still cannot find a way to accomplish this.

If I have an existing domain object that I want to use JAXB, for example, the following:

package com.blah.domain; class CustomerOffice{ private int id; private String name; private String phone; } 

And I have an XML schema as shown below:

 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:www="http://www.blah.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.blah.com" elementFormDefault="unqualified"> <xs:element name="Customer"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:int" nillable="false" minOccurs="1" maxOccurs="1"/> <xs:element name="name" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="CustomerOffice" type="www:CustomerOffice" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="CustomerOffice"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="length" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:schema> 

If I generate JAXB classes using xjc, it will create a new Customer class (which I want). It will also create a new CustomerOffice class (which I do not want, I want it to use my existing domain object).

So, instead of a schema pointing to "type: www: CustomerOffice", I would like it to use the existing com.blah.domain.CustomerOffice.

I tried to make this as simple as possible example, any help would be appreciated.

+8
xml schema jaxb bind xjc
source share
1 answer

You can use an external binding file to configure XJC to accomplish what you want.

 <jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> <jxb:bindings schemaLocation="yourSchema.xsd"> <jxb:bindings node="//xs:complexType[@name='CustomerOffice']"> <jxb:class ref="com.blah.domain.CustomerOffice"/> </jxb:bindings> </jxb:bindings> </jxb:bindings> 

XJC call

 xjc -d outputDir -b binding.xml yourSchema.xsd 
+11
source share

All Articles