Serialize a Tree of Java Objects into Custom XML -


imagine tree of java objects, i.e. top level object of class contains properties in turn objects of other classes , on until reach fundamental types integer or string, etc. example:

class car {   private door _leftdoor;   private door _rightdoor; }  class door {   private string _name; } 

by serializing top level object of class car, see xml document, example, this:

<object type="car">   <property type="door" identifier="_leftdoor">     <object type="door">       <property type="string" identifier="_name">i left door!</property>     </object>   </property>   <property type="door" identifier="_rightdoor">     <object type="door">       <property type="string" identifier="_name">i right door!</property>     </object>   </property> </object> 

by way, notice how fragment plugged other hierarchy, example, if car object property of other parent object.

my question is: right way implement patternwise, structurewise, designwise, architecturewise? think question needs clarification. first , simple way implement comes mind similar 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 idea in sense of adding kind of xml serialization directly methods of corresponding classes? i've restricted have element parameter (although i'm kind of forced because of way java xml api designed)? return element? have ideas improved architectural point of view in implementation? advice welcome.

i'm aware of java.beans.xmlencoder facility, java specific xml serializer , redundant me in case.

why not use xstream? if that's not applicable suggest use reflection api allow keep code generates xml separate. perhaps write generic method using reflection job rather writing specific xml serialization code each class. use custom annotations specify more granular behaviour within classes if necessary. feels re-inventing wheel because xstream can lot of you.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -