简体   繁体   中英

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:

在此处输入图片说明

Some help please?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM