How to construct objects based on XML code?

Posted by the_drow on Stack Overflow See other posts from Stack Overflow or by the_drow
Published on 2010-06-01T17:09:06Z Indexed on 2010/06/01 17:13 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

I have XML files that are representation of a portion of HTML code.
Those XML files also have widget declarations.
Example XML file:

 <message id="msg">
   <p>
     <Widget name="foo" type="SomeComplexWidget" attribute="value">
        inner text here, sets another attribute or 
        inserts another widget to the tree if needed...
     </Widget>
   </p>
 </message>

I have a main Widget class that all of my widgets inherit from.
The question is how would I create it?
Here are my options:

  1. Create a compile time tool that will parse the XML file and create the necessary code to bind the widgets to the needed objects.
    • Advantages:
      • No extra run-time overhead induced to the system.
      • It's easy to bind setters.
    • Disadvantages:
      • Adds another step to the build chain.
      • Hard to maintain as every widget in the system should be added to the parser.
      • Use of macros to bind the widgets.
      • Complex code
  2. Find a method to register all widgets into a factory automatically.
    • Advantages:
      • All of the binding is done completely automatically.
      • Easier to maintain then option 1 as every new widget will only need to call a WidgetFactory method that registers it.
    • Disadvantages:
      • No idea how to bind setters without introducing a maintainability nightmare.
      • Adds memory and run-time overhead.
      • Complex code

What do you think is better? Can you guys suggest a better solution?

© Stack Overflow or respective owner

Related posts about c++

Related posts about design