• Post Reply Bookmark Topic Watch Topic
  • New Topic

XML persistance design pattern.  RSS feed

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new to using XML with java so bear with me. This isn't an XML question as much as it is a design question. Say I have a class with a number of custom member variables:


If I want to Serialize Foo as an XML document, should I implement a class that specializes in turning Foo instances into XML documents, which includes looking at a Bar and turning it into an XML element and so forth. Or should I let each class be responsible for writing itself to an XMLDocument provided to it.
etc. How is this type of persistance normally handled?
[ July 10, 2006: Message edited by: Garrett Rowe ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for "Java XML Binding" and you'll find many tools that do exactly this. Sun's JAXB came up first on my search. Castor is another tool I see mentioned often. They follow your first approach with lots of reflection involved. It's good to focus your domain objects on a Single Responsibility in business terms, and keep the XML handling out of there.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following Stan's advice, I looked at Castor and it looks to be a relatively straightforward api on the surface. The issue I have with the *liberal* use of refelction to marshall/unmarshall objects is the fact that then I would have to have my classes conform to a particular style (read: public no-arg constructor, getXX/setXXX methods) for them to work properly. I also looked at the java.beans.XMLEncoder/XMLDecoder. With them at least i can subclass PersistanceDelegate to give me some control over the process. I understand your point completely about classes having a Single Responsibility, but I guess my feeling is that asking an object to marshall itself is akin to asking a Shape to draw itself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!