xjc: override xs:simpleType definition

前端 未结 2 1817
野的像风
野的像风 2021-01-23 02:32

I am compiling a set of XSDs to Java classes using xjc. I would like to be able to override the data type definition for a given simple type. The XSD snippet is:<

相关标签:
2条回答
  • 2021-01-23 02:49

    Further options are:

    • jaxb:baseType
    • jaxb:javaType
    • xjc:javaType - like jaxb:javaType but allows specifying the adapter class instead of unmarshal/marshal methods.

    I would actually argue that jaxb:class/@ref customization is not right for the simple type as this makes it a "class" type. This matters for the internal XJC model, some of the XJC plugins may handle your type incorrectly.

    I think you should use jaxb:javaType here. Try:

    <?xml version="1.0" encoding="UTF-8"?>
    <jxb:bindings jxb:version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="urn:your-target-namespace">
      <jxb:bindings node="/xs:schema" schemaLocation="../TCIP_4_0_0_Final.xsd">
        <jxb:globalBindings>
          <jxb:javaType name="javax.xml.datatype.XMLGregorianCalendar" xmlType="tns:CPT-DateTime"/>
        </jxb:globalBindings>
      </jxb:bindings>
    </jxb:bindings>
    
    0 讨论(0)
  • 2021-01-23 02:58

    This was actually remarkably easy once I found the right incantation; here's the bindings file:

    <?xml version="1.0" encoding="UTF-8"?>
    <jxb:bindings jxb:version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <jxb:bindings node="/xs:schema" schemaLocation="../TCIP_4_0_0_Final.xsd">
        <jxb:bindings node="//xs:simpleType[@name='CPT-DateTime']">
          <jxb:class ref="javax.xml.datatype.XMLGregorianCalendar"/>
        </jxb:bindings>
      </jxb:bindings>
    </jxb:bindings>
    
    0 讨论(0)
提交回复
热议问题