Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Java

Serializing Java objects to XML with WOX

Rate me:
Please Sign up or sign in to vote.
4.67/5 (6 votes)
26 Jul 2008GPL33 min read 43.6K   17   9
This article explains how to use WOX to serialize Java objects to XML. One of its main features is the generation of standard XML, which is language independant.

Introduction

This article explains how to use Web Objects in XML (WOX) to serialize Java objects to XML. Although WOX is also a serializer for C# objects, this article only concerns the serialization of Java objects. WOX is available at the WOX Serializer website.

WOX Main Features

Some of the WOX main features are listed below.

  • Easy to use. The Easy class provides serialization and de-serialization methods.
  • Simple XML. The XML generated is simple, easy to understand, and language independent.
  • Requires no class modifications. Classes do not require to have default constructors, getters, or setters.
  • Field visibility. Private fields are serialized just as any other field. WOX serializes fields regardless of their visibility.
  • Interoperability between Java and C#. WOX can serialize a Java object to XML, and reconstruct the XML back to a C# object; and vice versa.
  • Standard XML object representation. This could potentially allow to have WOX serializers in different object-oriented programming languages.
  • WOX data types. The WOX mapping table specifies how primitive data types are mapped to WOX data types.
  • Robust to class changes. Defaults will be used for newly added fields.
  • Arrays. Handles arrays and multi-dimensional arrays of primitives and objects.
  • Base-64. Byte arrays are base-64 encoded for efficiency.
  • Collection classes. Lists and Maps are provided as WOX data types. (ArrayList and HashMap in Java; ArrayList and Hashtable in C#.)
  • Object references. Handles duplicate and circular object references with id/idref.
  • Class and Type. Objects of these classes are saved by their string name.
  • Small footprint. The woxSerializer.jar file (which contains only .class files) is only 25K.

Using WOX

This is a quick introduction to the WOX serializer in Java. We will first create two classes. Then we will create some objects of those classes which will be serialized to XML. Next, we will have a look at the standard XML generated by WOX, and finally we will see how the XML goes back to a Java object.

Java
public class Student {
    private String name;
    private int registrationNumber;
    private Course[] courses;
    //constructors and methods omitted
}

public class Course {
    private int code;
    private String name;
    private int term;
    //constructors and methods omitted
}

Please notice that the fields in both classes are private. WOX does not take into consideration the visibility of the fields - they will be serialized regardless of their visibility. WOX in Java does not require that classes have default constructors, setters, or getters.

Serializing the Student Object to XML

We first create a student with some courses:

Java
Course[] courses = { new Course(6756, "XML and Related Technologies", 2),
                     new Course(9865, "Object Oriented Programming", 2),
                     new Course(1134, "E-Commerce Programming", 3) };
Student student = new Student ("Carlos Jaimez", 76453, courses);

We now use WOX to serialize the student to XML. We need to specify the file name where the student object will be stored.

Java
String filename = "student.xml";
Easy.save(student, filename);

The save method of the Easy class allows you to serialize an object to XML and store it to the specified XML file.

The resulting XML is shown below:

XML
<object type="Student" id="0">
   <field name="name" type="string" value="Carlos Jaimez" />
   <field name="registrationNumber" type="int" value="76453" />
   <field name="courses">
      <object type="array" elementType="Course" length="3" id="1">
         <object type="Course" id="2">
            <field name="code" type="int" value="6756" />
            <field name="name" type="string" value="XML and Related Technologies" />
            <field name="term" type="int" value="2" />
         </object>
         <object type="Course" id="3">
            <field name="code" type="int" value="9865" />
            <field name="name" type="string" value="Object Oriented Programming" />
            <field name="term" type="int" value="2" />
         </object>
         <object type="Course" id="4">
            <field name="code" type="int" value="1134" />
            <field name="name" type="string" value="E-Commerce Programming" />
            <field name="term" type="int" value="3" />
         </object>
      </object>
   </field>
</object>

The XML generated is a standard representation for the Student object. Every field is mapped to a field element, and every object is mapped to an object element. Also notice that the type attribute gives you the WOX data type of every field. The XML generated by WOX is simple, easy to understand, and language independent.

De-serializing the Student Object Back from XML

We will use the load method of the Easy class to de-seralize the Student object.

Java
Student newStudent = (Student)Easy.load(filename);

Done! The Student object has been reconstructed from XML to Java.

Summary

Web Objects in XML is an approach to serialize Java objects to XML, in a simple and robust way. The XML generated by WOX aims to be language independent, and easy to understand.

In this article, we have covered the following:

  • Explain WOX main features.
  • Create an object to be serialized.
  • Serialize an object to XML, by using the method Easy.save(Object obj, String filename).
  • De-serialize the object back from XML, by using the method Easy.load(String filename).

The last version of this XML serializer can be found at the WOX serializer website.

History

  • Release 1.0 - May 2008.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralEasy.load and Easy.save without disk access Pin
IonBro21-Jun-09 13:06
IonBro21-Jun-09 13:06 
GeneralRe: Easy.load and Easy.save without disk access Pin
AnthonyKilhoffer24-May-10 9:37
AnthonyKilhoffer24-May-10 9:37 
GeneralGreat idea! Pin
paul_mix29-Jul-08 4:34
paul_mix29-Jul-08 4:34 
This seems to be a good piece of work! I have only tested your examples in Java and C#, and they work well... I have also taken the XML of a serialized Java object and tried to put it back into a C# object and it really worked!...

I have a project in mind, which actually uses both Java and C#, and your serializer will reduce a lot of work. Thank you for sharing your code.

Paul
QuestionOnly Java?? Pin
sam.hill27-Jul-08 6:53
sam.hill27-Jul-08 6:53 
AnswerRe: Only Java?? Pin
sam.hill28-Jul-08 17:51
sam.hill28-Jul-08 17:51 
GeneralRe: Only Java?? Pin
Carlos Jaimez29-Jul-08 2:36
Carlos Jaimez29-Jul-08 2:36 
GeneralRe: Only Java?? Pin
sam.hill29-Jul-08 17:14
sam.hill29-Jul-08 17:14 
GeneralRe: Only Java?? Pin
sam.hill29-Jul-08 18:47
sam.hill29-Jul-08 18:47 
GeneralRe: Only Java?? Pin
dfsafdxcv6-Aug-08 3:14
dfsafdxcv6-Aug-08 3:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.