如何基于XML代码构造对象?
我有代表部分 HTML 代码的 XML 文件。
这些 XML 文件也有小部件声明。
XML 文件示例:
<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>
我有一个主 Widget 类,我的所有小部件都继承自该类。
问题是我如何创建它?
以下是我的选择:
- 创建一个编译时工具,该工具将解析 XML 文件并创建必要的代码以将小部件绑定到所需的对象。
- 优点:
- 不会给系统带来额外的运行时开销。
- 绑定 setter 很容易。
- 缺点:
- 向构建链添加另一个步骤。
- 难以维护,因为系统中的每个小部件都应添加到解析器中。
- 使用宏来绑定小部件。
- 复杂的代码
优点
- 优点:
- 找到一种方法将所有小部件自动注册到工厂中。
- 优点:
- 所有绑定都是完全自动完成的。
- 比选项 1 更容易维护,因为每个新的小部件只需要调用注册它的 WidgetFactory 方法。
- 缺点:
- 不知道如何在不引入可维护性噩梦的情况下绑定 setter。
- 增加内存和运行时开销。
- 复杂的代码
优点
- 优点:
你认为什么更好?你们能提出更好的解决方案吗?
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:
- 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
- Advantages:
- 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
- Advantages:
What do you think is better? Can you guys suggest a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建该工具,将其包含到您的构建步骤中,一切都会好起来的。
请参阅我之前答案的评论以了解更多详细信息。
Create the tool, include it into your build steps and everything will be fine.
See comments to my previous answer for additional details.
我不确定您发布的变体,但最简单的方法(不是最有效的,更像“get-it-done”方法)会写
boost::serialization
库的加载包装器。在这种情况下,您必须调用反序列化例程,指向文件(或其映射部分)并检索可以在小部件容器中注册的构造对象。
请注意,您可能不需要编写适当的保存(序列化)例程,只需编写加载例程。
另请注意,您可以将一些简单的(可能是自行实现的)解析器与您的反序列化例程结合起来,这样,例如第一个解析器就会获取与您的
Widget
相关的代码,并且第二个从中构造一个对象。I am not sure about the variants you've posted, but the easiest approach (not the most efficient, more like "get-it-done" approach) would be writing your loading wrapper for
boost::serialization
library.In this case you would have to call to your deserializing routine, point the file (or it's mapped part) and retrieve a constructed object that can be registered in your widget container.
Note that you probably won't need to write the appropriate saving (serializing) routine, only the loading one.
Also note that you could combine some easy (maybe self-implemented) parser with your deserializing routine, so that, for example the first one grabs the code related to your
Widget
and the second one constructs an object from it.