<WOX> Web Objects in XML </WOX>

  Efficient and easy XML serialization of Java and C# objects

Lists

Overview

Lists are similar to arrays in WOX. A List in WOX is the equivalent to java.util.ArrayList in Java, and System.Collections.ArrayList in C#, as can be seen in the Data types mapping section. A List is represented in a unique way, which means that it can be de-serialized either to Java or C#.

The code for the example in this section is provided in both Java and C#.

The Course class

This is the Course class, which has three fields: code of type int, name of type string, and term of type int.

//Java
public class Course {
    private int code;
    private String name;
    private int term;
    //constructors and methods omitted
}
//C#
public class Course {
    private In32 code;
    private String name;
    private Int32 term;
    //constructors and methods omitted
}

Serializing a list of Course objects to XML

A list of four Course objects is created below.

//Java
ArrayList list = new ArrayList();
list.add(new Course(6756, "XML and Related Technologies", 3));
list.add(new Course(9865, "Object Oriented Programming", 2));
list.add(new Course(1134, "E-Commerce Programming", 2));
list.add(new Course(4598, "Enterprise Component Architecture", 3));
//C#
ArrayList list = new ArrayList();
list.Add(new Course(6756, "XML and Related Technologies", 3));
list.Add(new Course(9865, "Object Oriented Programming", 2));
list.Add(new Course(1134, "E-Commerce Programming", 2));
list.Add(new Course(4598, "Enterprise Component Architecture", 3));

We use the save method of the Easy class to serialize the list of Course objects to XML, and store it to the specified file.

In Java you will require woxSerializer.jar and jdom.jar files in the classpath. In C# you will require woxSerializer.dll.

//Java
String filename = "listCourses.xml";
Easy.save(list, filename);
//C#
String filename = "listCourses.xml";
Easy.save(list, filename);

The XML representation of the list is shown below.

<object type="list" elementType="Object" length="4" id="0">
    <object type="Course" id="1">
        <field name="code" type="int" value="6756" />
        <field name="name" type="string" value="XML and Related Technologies" />
        <field name="term" type="int" value="3" />
    </object>
    <object type="Course" id="2">
        <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="3">
        <field name="code" type="int" value="1134" />
        <field name="name" type="string" value="E-Commerce Programming" />
        <field name="term" type="int" value="2" />
    </object>
    <object type="Course" id="4">
        <field name="code" type="int" value="4598" />
        <field name="name" type="string" value="Enterprise Component Architecture" />
        <field name="term" type="int" value="3" />
    </object>
</object>

The root is an object element, which is the list of four Course objects. Each course has its type, and id attributes, and its three fields (children): code, name, and term. Every field specifies its name, type, and value attributes. The XML is simple.

De-serializing a list of Course objects back from XML

Since the list is not specific to Java or C#, we can use the load method of the Easy class de-seralize the list back from XML either to Java or C#. Note that WOX in Java does not require the Course class to have a default constructor; but WOX in C# does.

//Java
ArrayList newList = (ArrayList)Easy.load(filename);
//C#
ArrayList newList = (ArrayList)Easy.load(filename);

The list has been reconstructed and you can now iterate to display its elements.