如何让XJC将XSD无类型元素生成为String而非Object?
问题:将XSD中未指定类型的元素生成为String而非Object类型
背景
使用XJC配合maven-jaxb2-plugin生成JAXB类时,XSD中未指定类型的元素默认会被生成为Java的Object类型,需改为String类型,且尽量不修改原XSD文件。
当前配置
绑定文件(binding.xjb)
<jxb:bindings version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"> <jxb:globalBindings typesafeEnumMaxMembers="1000" collectionType="java.util.ArrayList"> <xjc:simple/> </jxb:globalBindings> <jxb:bindings schemaLocation="../xsd/SOME_OTHER_XSD.xsd"> <jxb:bindings node="//xs:complexType[@name='SOME_OTHER_TYPE']"> <jxb:bindings node=".//xs:attribute[@name='Version']"> <jxb:property name="VersionAttribute"/> </jxb:bindings> </jxb:bindings> </jxb:bindings> <jxb:bindings schemaLocation="../xsd/XSD_IN_QUESTION.xsd"> <jxb:bindings node="//xs:complexType[@name='TYPE_IN_QUESTION']"> <jxb:bindings node=".//xs:element[@name='Version']"> <jxb:javaType name="java.lang.String" parseMethod="javax.xml.bind.annotation.adapters.CollapsedStringAdapter"/> </jxb:bindings> </jxb:bindings> </jxb:bindings> </jxb:bindings>
XSD文件结构
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns2="ns2" xmlns="ns" targetNamespace="ns" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="PARENT_ELEMENT" type="TYPE_IN_QUESTION"/> <xs:complexType name="TYPE_IN_QUESTION"> <xs:sequence> <xs:element name="Version"/> <!-- No Type specified --> </xs:sequence> </xs:complexType> </xs:schema>
遇到的错误
[ERROR] Error while parsing schema(s).Location [ file:/C:/[PATH]/src/main/resources/META-INF/binding/binding.xjb{25,128}]. com.sun.istack.SAXParseException2: compiler was unable to honor this javaType customization. It is attached to a wrong place, or its inconsistent with other bindings. ... [ERROR] Error while parsing schema(s).Location [ file:/C:/[PATH]/src/main/resources/META-INF/xsd/XSD_IN_QUESTION.xsd{93,41}]. com.sun.istack.SAXParseException2: (the above customization is attached to the following location in the schema)
解决方案
错误原因
XSD中未指定类型的元素默认对应xsd:anyType,直接在元素节点上使用<jxb:javaType>是无效的,该自定义绑定不能直接附着在元素节点上。
1. 单个元素修复方案
在绑定文件中,针对目标元素通过<jxb:baseType>指定其基础类型为xs:string:
<jxb:bindings schemaLocation="../xsd/XSD_IN_QUESTION.xsd"> <jxb:bindings node="//xs:complexType[@name='TYPE_IN_QUESTION']"> <jxb:bindings node=".//xs:element[@name='Version']"> <jxb:property> <jxb:baseType name="xs:string"/> </jxb:property> </jxb:bindings> </jxb:bindings> </jxb:bindings>
2. 全局解决方案(推荐)
如果需要所有未指定类型的元素都生成为String,修改<globalBindings>,添加全局类型映射,将xsd:anyType直接映射为java.lang.String:
<jxb:globalBindings typesafeEnumMaxMembers="1000" collectionType="java.util.ArrayList"> <xjc:simple/> <!-- 全局映射xsd:anyType到String,同时使用CollapsedStringAdapter处理字符串 --> <xjc:javaType name="java.lang.String" xmlType="xs:anyType" parseMethod="javax.xml.bind.annotation.adapters.CollapsedStringAdapter.unmarshal" printMethod="javax.xml.bind.annotation.adapters.CollapsedStringAdapter.marshal"/> </jxb:globalBindings>
该配置会自动将所有XSD中未指定类型的元素生成为String类型,无需逐个元素配置。
内容的提问来源于stack exchange,提问作者Jonas Seidl
相关产品推荐
相关产品推荐

