0

When generating Java classes from a XSD, how can I specify that for some specific node, a specific and already existent Java class should be used instead of trying to generate one?

Thank you very much.

Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77

2 Answers2

1

You should use following binding customization

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net" xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="../schema/yourSchema.xsd">

        <bindings node="//xs:complexType[@name='Foo']">
            <class ref="com.FooImpl"/> 
        </bindings>

    </bindings>
</bindings>
Xstian
  • 8,184
  • 10
  • 42
  • 72
1

You can use episode file to reference the existing classes. .episode files are just jaxb bindings file and has mappings between elements and java classes.

a) if those existing classes are also generated from (another) xsd. use below option to first create .episode file.

xjc -episode a.episode a.xsd

then use this a.episode that contains the mappings as input to the next xjc generation.

xjc b.xsd -extension -b a.episode

b) If you want to refer some random classes, then you may have to write your own episode file providing mapping between element and class reference like below.

sample.episode

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
 <jaxb:bindings scd="x-schema::">
    <jaxb:bindings scd="employee">
      <jaxb:class ref="www1.example.Employee"/>
      <jaxb:package name="www1.example" />
    </jaxb:bindings>
   </jaxb:bindings>

and use xjc b.xsd -extension -b sample.episode

ulab
  • 1,079
  • 3
  • 15
  • 45
  • Thank you very much. This is for the 2+ version, do you know if it is possible to do it for Jaxb2 version 1.6? – Mr.Eddart Sep 13 '16 at 14:06
  • welcome!. yes it is possible using JDK 1.6 (update latest). Refer this [matrix](https://jaxb.java.net/guide/Which_JAXB_RI_is_included_in_which_JDK_.html) – ulab Sep 13 '16 at 14:15