com.topologi.diffx.xml
Class XMLSerializer

java.lang.Object
  extended by com.topologi.diffx.xml.XMLSerializer

public final class XMLSerializer
extends Object

This class provides methods to serialize objects to XML.

There is no mechanism to prevent infinite loops if some objects (lists,...) reference themselves.

The underlying XML document is generated using an XML string buffer.

Version:
7 March 2005
Author:
Christophe Lauret

Constructor Summary
XMLSerializer(XMLWriter xml)
          Creates a new XML serializer using the specified XML writer.
 
Method Summary
 XMLWriter getXML()
          Returns the underlying XML document.
 void serialize(Object o, String name)
          Serialises the given object using the given name as element name.
 void serializeCollection(Collection<?> c)
          Serialises the given Collection to xml.
 void serializeHashtable(Hashtable<?,?> h)
          Serialise the given Hashtable to xml.
 void serializeObject(Object o)
          Serialises the given object to xml by using the public methods getXXX().
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLSerializer

public XMLSerializer(XMLWriter xml)
Creates a new XML serializer using the specified XML writer.

Parameters:
xml - The XML string buffer to be used
Method Detail

getXML

public XMLWriter getXML()
Returns the underlying XML document.

Returns:
the xml stringbuffer

serialize

public void serialize(Object o,
                      String name)
               throws IOException
Serialises the given object using the given name as element name.

This implementation is recursive. It calls itself for fields which are not of primitive type.

Parameters:
o - Object to be serialised as xml
name - Name of object
Throws:
IOException - Should an I/O error occur.

serializeCollection

public void serializeCollection(Collection<?> c)
                         throws IOException
Serialises the given Collection to xml.

Iterates over every object and call the serialize(java.lang.Object, java.lang.String) method.

Parameters:
c - The Collection to be serialised to XML
Throws:
IOException - Should an I/O error occur.

serializeHashtable

public void serializeHashtable(Hashtable<?,?> h)
                        throws IOException
Serialise the given Hashtable to xml.

This methods only works if the Hashtable contains String objects.

Parameters:
h - The hashtable to be serialized to XML
Throws:
IOException - Should an I/O error occur.

serializeObject

public void serializeObject(Object o)
                     throws IOException
Serialises the given object to xml by using the public methods getXXX().

This method calls every getXXX() method from the object to get the returned object and then calls the serialize(Object, String) method with the returned object and the name xxx in lower case.

Parameters:
o - The object to be serialised as XML
Throws:
IOException - Should an I/O error occur.