Linux 如何从实现可序列化的 XSD 生成类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:14:38  来源:igfitidea点击:

How to generate classes from XSD that implements serializable?

javaeclipseserializationxsdjaxb

提问by Topera

I need to generate many classes from my XML Schema (XSD) in a package (.jar). How can I configure these classes to be serializable?

我需要从包 (.jar) 中的 XML 模式 (XSD) 生成许多类。如何将这些类配置为可序列化?

(I'm using Eclipse and JAX-B)

(我正在使用 Eclipse 和 JAX-B)

采纳答案by Tomas Narros

If you are using XJC, I recomend you to read this reference: JavaTM Architecture for XML Binding: JAXB RI Vendor Extensions Customizations :

如果您使用 XJC,我建议您阅读此参考资料:JavaTM Architecture for XML Binding:JAXB RI Vendor Extensions Customizations

You have to add in your schema aditional namespaces definition to add xjc aditional markup:

您必须添加架构附加命名空间定义以添加 xjc 附加标记:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
           jaxb:extensionBindingPrefixes="xjc"
           jaxb:version="1.0">

Then, including an <xjc:serializable>node within <jaxb:globalBindings>:

然后,在 中包含一个<xjc:serializable>节点<jaxb:globalBindings>

<xs:annotation>
   <xs:appinfo>
      <jaxb:globalBindings generateIsSetMethod="true">
          <xjc:serializable uid="12343"/>
      </jaxb:globalBindings>
   </xs:appinfo>
</xs:annotation>

This will cause that all the concrete classes implement the Serializable interface. Also, you can define the UUID value of the resulting classes (that's an optional attribute).

这将导致所有具体类都实现 Serializable 接口。此外,您可以定义结果类的 UUID 值(这是一个可选属性)。

回答by Topera

I've found

我找到了

<schema
  xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  jaxb:extensionBindingPrefixes="xjc"
  jaxb:version="1.0"  
  >

  <!-- FORCE ALL CLASSES IMPLEMENTS SERIALIZABLE -->
  <annotation>
    <appinfo>
      <jaxb:globalBindings generateIsSetMethod="true">
        <xjc:serializable uid="1"/>
      </jaxb:globalBindings>
    </appinfo>
  </annotation>

   ....

</schema>