<WOX> Web Objects in XML </WOX>

  Efficient and easy XML serialization of Java and C# objects

Object arrays

Overview

We will cover object arrays in this section. The main difference between primitive arrays and object arrays is that the elements in primitive arrays are serialized in a string separated by spaces, whereas object arrays cannot be serialized in the same way, because their elements have a more complex structure. Every element in an object array is represented as a separate object as you will see in the serialization section on this page.

Object arrays can also be unidimensional or multi-dimensional, just like primitive arrays. We will only show you the XML representation of a unidimensional object array, but you can try with your own multi-dimensional object arrays (they will follow the same idea shown in the N-D Primitive Arrays section). The code is provided in Java and C#.

The Product class

This is the Product class, which has five fields: name of type string, price of type double, grams of type int, registered of type boolean, and category of type char.

//Java
public class Product {
    private String name;
    private double price;
    private int grams;
    private boolean registered;
    private char category;
    //constructors and methods omitted
}
//C#
public class Product {
    private String name;
    private double price;
    private int grams;
    private bool registered;
    private char category;
    //constructors and methods omitted
}

Serializing an array of Product objects to XML

An array of three Product objects is created below.

//Java
Product[] products = new Product[]{new Product("Baked beans", 1.75, 250, true, 'B'),
                                   new Product("Basmati Rice", 3.89, 750, true, 'R'),
                                   new Product("White bread", 1.06, 300, false, 'H')};
//C#
Product[] products = new Product[]{new Product("Baked beans", 1.75, 250, true, 'B'),
                                   new Product("Basmati Rice", 3.89, 750, true, 'R'),
                                   new Product("White bread", 1.06, 300, false, 'H')};

We use the save method of the Easy class to serialize our array of Product 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 = "arrayProducts.xml";
Easy.save(products, filename);
//C#
String filename = "arrayProducts.xml";
Easy.save(products, filename);

The array represented in XML is shown below. The array in Java and the array in C# have the same XML representation.

<object type="array" elementType="Product" length="3" id="0">
    <object type="Product" id="1">
        <field name="name" type="string" value="Baked beans" />
        <field name="price" type="double" value="1.75" />
        <field name="grams" type="int" value="250" />
        <field name="registered" type="boolean" value="true" />
        <field name="category" type="char" value="\u0042" />
    </object>
    <object type="Product" id="2">
        <field name="name" type="string" value="Basmati Rice" />
        <field name="price" type="double" value="3.89" />
        <field name="grams" type="int" value="750" />
        <field name="registered" type="boolean" value="true" />
        <field name="category" type="char" value="\u0052" />
    </object>
    <object type="Product" id="3">
        <field name="name" type="string" value="White bread" />
        <field name="price" type="double" value="1.06" />
        <field name="grams" type="int" value="300" />
        <field name="registered" type="boolean" value="false" />
        <field name="category" type="char" value="\u0048" />
    </object>
</object>

The root is an object element, which is an array of three Product objects. Each product has its type, and id attributes, and its five fields (children): name, price, grams, registered, and category. Every field specifies its name, type, and value attributes. The XML is simple and easy to understand.

De-serializing an array of Product objects back from XML

We can use the load method of the Easy class de-seralize the array back from XML either to Java or C#. Note that WOX in Java does not require the Product class to have a default constructor; but WOX in C# does.

//Java
Product[] newProducts = (Product[])Easy.load(filename);
//C#
Product[] newProducts = (Product[])Easy.load(filename);

The array of Product objects has been reconstructed and you can now iterate to display its elements.