简体   繁体   English

JAXB绑定以覆盖或删除XMLSchemaType xs:token

[英]JAXB binding to override or remove XMLSchemaType xs:token

I am generating model classes from an xsd using the maven jaxb2 plugin. 我正在使用Maven jaxb2插件从xsd生成模型类。 The xsd specifies certain elements with type=xs:token as follows: xsd指定具有type = xs:token的某些元素,如下所示:

<xs:element name="medium_text" type="xs:token"/>

This results in the following annotations being added to the field in the generated model: 这导致将以下注释添加到生成的模型的字段中:

@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name="token")
protected String medium_text;

The problem is the CollapsedStringAdapter removes new lines etc (which is the correct behaviour for xs:token) but I want to avoid this. 问题是CollapsedStringAdapter删除了新行等(这是xs:token的正确行为),但我想避免这种情况。

How can remove the xs:token type from the element or change it to xs:string using JAXB bindings? 如何使用JAXB绑定从元素中删除xs:token类型或将其更改为xs:string? The easy solution is to remove the type from the xsd but is it possible using bindings without modifying the xsd? 一种简单的解决方案是从xsd中删除类型,但是可以使用绑定而不修改xsd吗?

You can use the following in your binding file. 您可以在绑定文件中使用以下内容。

<jxb:bindings schemaLocation = "schema.xsd">
   <jxb:bindings node = "//xs:element[@name='medium_text']">
             <jxb:javaType name="java.lang.String"/> 
   </jxb:bindings>
</jxb:bindings>

This results in the following: 结果如下:

@XmlElement(name = "mid_text", required = true)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "token")
protected String midText;

Adapter1 however is harmless and does not change the string value. 但是,Adapter1是无害的,并且不会更改字符串值。

Also if you want to do this to all your tokens you can add a globalBinding 另外,如果要对所有tokens执行此操作,则可以添加globalBinding

  <jxb:globalBindings>
            <jxb:javaType name="java.lang.String"
                xmlType="xs:token" />
  </jxb:globalBindings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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