Categories
Coding

XML Serialization in C# and Java

I recently was looking at the JavaBean-related XML serialization functionality (see the XMLEncoder class for details). While I was at it, I also decided to see how this was handled in .Net.

For starters, check out the following C# classes:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyApplication
{
public class MyClass
{
public String name;
public MyOtherClass other;

}

public class MyOtherClass {
public string name;
}
}

And their Java equivalents:

public class MyClass {
private String name;
private MyOtherClass other;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public MyOtherClass getOther() {
return other;
}

public void setOther(MyOtherClass other) {
this.other = other;
}

}

public class MyOtherClass {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

Native XML Serialization in .Net is as easy as this:

MyClass clazz = new MyClass();
clazz.name = "Hello World";
clazz.other = new MyOtherClass();
clazz.other.name = "Test";
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(clazz.GetType());
writer.Serialize(new System.IO.StreamWriter("c:\\temp\\test.xml"), clazz);

On the Java side, it is also pretty trivial:

MyClass clazz1 = new MyClass();
clazz1.setName("Hello, World!");

MyOtherClass clazz2 = new MyOtherClass();
clazz2.setName("Testing");
clazz1.setOther(clazz2);

XMLEncoder encoder = new XMLEncoder(new FileOutputStream("test.xml"));
encoder.writeObject(clazz1);
encoder.close();

The XML file produced by .Net serialization looks like the following. Note that it does not contain any datatype information, relying solely on reflection in order to determine the correct datatypes.

<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<name>Hello World</name>
<other>
<name>Test</name>
</other>
</MyClass>

The Java-produced XML file looks like this. It is generally more verbose than the .Net equivalent, and contains package and datatype information. Note that XMLEncoder will also ignore transient fields.

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_05" class="java.beans.XMLDecoder">
<object class="uk.co.researchkitchen.soap.fx.MyClass">
<void property="name">
<string>Hello, World!</string>
</void>
<void property="other">
<object class="uk.co.researchkitchen.soap.fx.MyOtherClass">
<void property="name">
<string>Testing</string>
</void>
</object>
</void>
</object>
</java>

Overall, the built-in Java serializer seems to be more powerful, or at least more customizable than, its .Net counterpart. It follows from the above tools that, in theory, you could implement a cheap Java/C# serialization system using nothing more sophisticated than the built-in serialization classes and a couple of XSLT stylesheets.