1

I'm getting the error message:

Could not convert xsd:date to java.lang.String type

I'm using a binding.xml file with CXF XJC plugin

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Date" type="xsd:date"/>
    <xsd:element name="Audit">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Creation">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element ref="Date"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

binding.xml

<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:xsd="http://www.w3.org/2001/XMLSchema">
    <jaxb:bindings>
        <jaxb:globalBindings>
            <jaxb:javaType name="java.lang.String" xmlType="xsd:date"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings> 

cxf xjc plugin:

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-xjc-plugin</artifactId>
        <version>3.0.3</version>
        <executions>
            <execution>
                <id>generate-resources</id>
                <phase>generate-resources</phase>
                <configuration>
                    <defaultOptions>
                        <bindingFiles>
                            <bindingFile>${basedir}/src/main/resources/META-INF/binding.xml</bindingFile>
                        </bindingFiles>
                        <noAddressBinding>true</noAddressBinding>
                    </defaultOptions>
                    <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                    <xsdOptions>
                        <xsdOption>
                            <xsd>${basedir}/src/main/resources/xsd/Misc.xsd</xsd>
                            <packagename>com.mycomp.ext.schema</packagename>
                            <extension>true</extension>
                        </xsdOption>
                    </xsdOptions>
                </configuration>
                <goals>
                    <goal>xsdtojava</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Generated code contains XMLGregorianCalendar instead of String

    @XmlElement(name = "Date", required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar date;

Any suggestions please?

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
Sparkle8
  • 225
  • 3
  • 13
  • [If that is the case you want to use the schema conversion then you can try the solution given in the link][1] [1]: http://stackoverflow.com/questions/3679266/simple-conversion-between-java-util-date-and-xmlgregoriancalendar – vinothM Jun 11 '15 at 22:33

2 Answers2

0

If you change the xml type to xsd:date xsd:string it should be working

vinothM
  • 195
  • 1
  • 6
  • I have number of XSDs and with binding file I want to convert xsd:data fields as String type in generated java POJOs. – Sparkle8 Jun 11 '15 at 17:21
0

Sorry I mean if you need to access it as string field in java you should define that schema with xsd:string type instead of the xsd:date type.

Option one: If you want to use the field as date in java you can format it in java later.
Option two: If you want to make schema to take care of data type you should define as xsd:date and use the XMLGregorianCalendar which can be converted to Calendar or to normal Date java object later in java. This way the date format validation will be done by schema itself.

vinothM
  • 195
  • 1
  • 6