简体   繁体   English

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

[英]How to generate classes from XSD that implements serializable?

I need to generate many classes from my XML Schema (XSD) in a package (.jar). 我需要在包(.jar)中从我的XML Schema(XSD)生成许多类。 How can I configure these classes to be serializable? 如何将这些类配置为可序列化?

(I'm using Eclipse and JAX-B) (我正在使用Eclipse和JAX-B)

If you are using XJC, I recomend you to read this reference: JavaTM Architecture for XML Binding: JAXB RI Vendor Extensions Customizations : 如果您正在使用XJC,我建议您阅读此参考: 用于XML绑定的JavaTM体系结构:JAXB RI供应商扩展自定义

You have to add in your schema aditional namespaces definition to add xjc aditional markup: 您必须添加架构aditional命名空间定义以添加xjc aditional标记:

<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> : 然后,在<jaxb:globalBindings>包含<xjc:serializable>节点:

<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. 这将导致所有具体类实现Serializable接口。 Also, you can define the UUID value of the resulting classes (that's an optional attribute). 此外,您可以定义结果类的UUID值(这是一个可选属性)。

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>

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

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