Jordi
Jordi

Reputation: 23277

JAXB XJC: how to generate java classes

I'm trying to use xjc tool in order to generate java code according to some schemas.

I'm performing this command:

xjc -d generated -p net.mycom.tech .

First question is,

  1. What -b bindings.xjb is meant for? Which content should it has to have? Is it mandatory?
  2. I'm getting these two kind of error messages:

    a. [ERROR] Two declarations cause a collision in the ObjectFactory class.

    b. A class/interface with the same name "net.mycom.tech.DatosGenericos" is already in use. Use a class customization to resolve this conflict.

My folder is containing several xsd:

enter image description here

Some help please?

Upvotes: 1

Views: 9610

Answers (1)

Sambit
Sambit

Reputation: 8031

In case of XJC compiler, -b is used for external JAXB binding which overrides the default rule. As an example find below a small snippet. The name of this file is mybind.xjb.

<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    jaxb:extensionBindingPrefixes="xjc">

    <jaxb:globalBindings>
        <xjc:simple />
        <xjc:serializable uid="-1" />
        <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
            parse="javax.xml.bind.DatatypeConverter.parseDateTime"
            print="javax.xml.bind.DatatypeConverter.printDateTime" />
    </jaxb:globalBindings>
</jaxb:bindings>

In this case mybind.xjb above overrides the dateTime type to the java.util.Calendar type

For more details, you can refer below the oracle documentation link. https://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

As far the error is concerned, I would recommend to generate the classes to a new directory which is empty. You have to use the following command

xjc -d src -p com.projectname.place.functionality xmlSchema.xsd

In the above case ensure that the directory src does not contain any java files.

Upvotes: 0

Related Questions