Search code examples
xsdxml-binding

Jaxb binding attribute to string without adapter


Is it possible to have a binding to a string without an adapter?

I have a 3rd party schema with an attribute with the wrong type (date) and in an external file binding I transform it to string so the class is generated with the correct type. The "problem" is that it does so through a new class "Adapter1" which is very trivial and I prefer it not to be generated.

The attribute is as:

      <xs:attribute  type="xs:date" name="curso" use="required">
      </xs:attribute>

And the current binding is:

<jxb:bindings 
    version="1.0"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings 
            schemaLocation="file.xsd" 
            node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='exportacion_horarios']//xs:attribute[@name='curso']" >
            <jxb:property>
                <jxb:baseType >
                    <jxb:javaType name="java.lang.String" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

The adapter class is:

package com.penalara.ghc_importar_exportar.modelo.personalizados.itaca3.exportacion;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class Adapter1
    extends XmlAdapter<String, String>
{


    public String unmarshal(String value) {
        return new String(value);
    }

    public String marshal(String value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }

}

Maybe something like telling "xjc" to use the internal default adapter for string-to-string.


Solution

  • You can do it by not using jxb:javaType and putting the name directly in the jxb:baseType.

    <jxb:bindings 
        version="1.0"
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jxb:bindings 
                schemaLocation="file.xsd" 
                node="/xs:schema">
            <jxb:bindings node="//xs:element[@name='exportacion_horarios']//xs:attribute[@name='curso']" >
                <jxb:property>
                    <jxb:baseType name="java.lang.String"/>
                </jxb:property>
            </jxb:bindings>
        </jxb:bindings>
    </jxb:bindings>
    
    

    This way it doesn't generate an adapter and uses Java String.