Imagine a tree of Java objects, i.e. a top level object of some class which contains some properties which in turn are objects of other classes and so on until we basically reach fundamental types like integer or string, etc. For example:
class Car {
private Door _leftDoor;
private Door _rightDoor;
}
class Door {
private String _name;
}
By serializing a top level object of class Car, I would like to see an XML document, for example, like this:
<object type="Car">
<property type="Door" identifier="_leftDoor">
<object type="Door">
<property type="String" identifier="_name">I am a left door!</property>
</object>
</property>
<property type="Door" identifier="_rightDoor">
<object type="Door">
<property type="String" identifier="_name">I am a right door!</property>
</object>
</property>
</object>
By the way, notice how this fragment could be plugged into some other hierarchy, for example, if Car object would be property of some other parent object.
My question is: what would be the right way to implement this patternwise, structurewise, designwise, architecturewise? I think my question needs some clarification. The first and simple way to implement this that comes to my mind is something similar to Java's conventional toString:
class Car {
...
public Element toElement(Element element) {
Document document = element.getOwnerDocument();
Attr attr;
Element objectElement = document.createElement("object");
element.appendChild(objectElement);
attr = document.createAttribute("type");
attr.setValue(class.getSimpleName());
objectElement.setAttributeNode(attr);
Element propertyElement;
propertyElement = document.createElement("property");
objectElement.appendChild(propertyElement);
attr = document.createAttribute("type");
attr.setValue(_leftDoor.getClass().getSimpleName());
propertyElement.setAttributeNode(attr);
attr = document.createAttribute("identifier");
attr.setValue("_leftDoor");
propertyElement.setAttributeNode(attr);
_leftDoor.toElement(propertyElement);
propertyElement = document.createElement("property");
objectElement.appendChild(propertyElement);
attr = document.createAttribute("type");
attr.setValue(_rightDoor.getClass().getSimpleName());
propertyElement.setAttributeNode(attr);
attr = document.createAttribute("identifier");
attr.setValue("_rightDoor");
propertyElement.setAttributeNode(attr);
_rightDoor.toElement(propertyElement);
return objectElement;
}
}
class Door {
...
public Element toElement(Element element) {
...
}
}
How good is this idea in the sense of adding this kind of XML serialization directly as methods of corresponding classes? Is it good that I've restricted it to have Element as parameter (although I'm kind of forced to do it because of the way Java XML API is designed)? Is it good that I return Element? Do you have any ideas what could be improved from the architectural point of view in this implementation? Any advice is welcome.
I'm aware of the java.beans.XMLEncoder facility, but this is Java specific XML serializer and redundant for me in this case.