1

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.

1 Answer 1

2

Why not use something like XStream? If that's not applicable then I would suggest you use the reflection API to allow you to keep the code that generates the XML separate. Perhaps you could write a generic method using reflection that would do the job rather than writing specific XML serialization code for each class. You could use custom annotations to specify more granular behaviour within your classes if necessary. But it feels like re-inventing the wheel because something like XStream can probably do a lot of that for you.

Sign up to request clarification or add additional context in comments.

1 Comment

XStream is indeed a very interesting piece of software, purely generic XML marshalling/unmarshalling. I've also found out that JAXB, which is now bundled with Java SE, is another alternative to do the same and even more! The How Does JAXB Compare to XStream? article was particularly good to explain that. I think I will be able to somehow adapt this approach to what I want to achieve as I'm not a wheel reinvention fan anyway. So thanks for pointing this out, I knew that somebody has already done much better than me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.