1

I have an xml data. From this data I need to create objects. Till now I used one class for parsing xml data and managing object state. I can see that cohesion of class is very low. So I want to give parsing responsibility to another class. As a simple idea I may use Factory Pattern for xml parsing and creating objects from xml data(My objects have a common base class). But how should I return created objects to consumer class. Container class object instance(list,map etc.)? One by one(when a node parsed)?

I am open to different desing advices.

onurozcelik
  • 1,214
  • 3
  • 21
  • 44

2 Answers2

2

You may want to check libraries that do things like this for you. For instance simple: http://simple.sourceforge.net or JAXB: http://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding

yankee
  • 38,872
  • 15
  • 103
  • 162
  • I do not need to use that kind of library. I already have classes for this task. Check my comment at second answer. – onurozcelik Feb 04 '11 at 08:52
  • @onurozcelik: Sorry, I guess than that I still did not catch your question. You are asking how to return objects (that you just unserialized from an xml file) to consumer classes. How is your overall approach? Is it like a big configfile as singleton where "everyone" can pull its config from? Or does (un)serializing happen all the time? Do you buffer unserialized data? Is it an option? Do classes poll tags? – yankee Feb 04 '11 at 12:54
2

There's got to be an existing parser for your language/platform. My advice is to ask yourself if you really need to reinvent something like this.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • I think you got me wrong. Sorry for my poor explanation. I am not going to write DOMParser or SAXParser class. Assume I have employee data in xml format and need to create Employee objects from this data. I do not want to give xml reading and employee object creation responsibility to one class. How should I design my classes. My question is exactly this. – onurozcelik Feb 04 '11 at 08:43
  • @onurozcelik - I believe Sean's answer is directly addressing this point. He is suggesting that there must be a JAXB (Java) equivalent in C++ for handling the object-to-XML conversion. – bdoughan Feb 18 '11 at 16:15
  • Although this does not answer your question, it is absolutely related and good to know: http://stackoverflow.com/questions/87621/how-do-i-map-xml-to-c-sharp-objects – Chinmoy Oct 08 '12 at 06:09