Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate classes from XSD that implements serializable?

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

(I'm using Eclipse and JAX-B)

like image 917
Topera Avatar asked Sep 09 '10 13:09

Topera


People also ask

What happens if a class implements Serializable?

If a super class implements Serializable, then its sub classes do automatically. When an instance of a serializable class is deserialized, the constructor doesn't run. If a super class doesn't implement Serializable, then when a subclass object is deserialized, the super class constructor will run.

Which class should implements Serializable?

awt. Button class implements the Serializable interface, so you can serialize a java. awt. Button object and store that serialized state in a file.


2 Answers

If you are using XJC, I recomend you to read this reference: 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:

<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>:

<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).

like image 140
Tomas Narros Avatar answered Sep 28 '22 04:09

Tomas Narros


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>
like image 35
Topera Avatar answered Sep 28 '22 03:09

Topera