What is the best way to create C++ objects from parsed XML data?

Started by
7 comments, last by vovansim 19 years, 8 months ago
I'm working on a 3d engine system. I need to save/load data in the systems objects to/from XML files. When I say objects, I mean C++ objects. (These generally correspond to scene objects, but that consideration isn't important as far as loading/saving goes.) I think I have a reasonable grasp of XML, it's method of data representation, and parsing XML files. (At the moment I'm looking at using the expat XML parser for it's callback based system, so I don't have to load/parse the whole - potentially v large - XML file into memory before I create objects). The problem I'm having is deciding how to get the data from the XML parser into the C++ objects. How is this normally done? Do you pass the completed/parsed XML node to the C++ object creator? Do you create the object object and set it's members with data from the completed/parsed XML node? What's the best/most flexible way to do this, or are what are pros and cons of various methods? Pointers to some examples would be greatly appreciated. Thanks Jim Williams
Advertisement
Check out Enginuity's Serialization stuff. That could easily be adapted to an XML base.
daerid@gmail.com
Not sure if this is the type of answer you're looking for, but I'm just using simple strstr() functionality. My 3D tool of choice is Cinema4D which can export scenes to and XML doc. Since XML is just a text doc, I thought it was easier just to open the file, use strstr() to locate the tags i'm looking for, then save the vertex, face, texture path...etc data to my own 3d file format.

As an alternative C++.Net has built in XML read/export classes, and look very easy to use, although I haven't tried them myself yet.

Goodluck.
----------------------------------------------------
Check out my casual TBS game blog
----------------------------------------------------
I do:

Element = Object
Value = MemberVariable

This means an object can be decomposed to :

CObject
std::string Name
std::map<std::string, std::string> KeyValue;

Write a simple XML parser to find the elements. Then use a factory to create the object. Assign the key-value pair to the object.

I then use a simple tree class to manage the shape of the data. Best part of this is that it's extremely simple to turn a in game tree of objects back in to xml.
Chris Brodie
>I do:
>
>Element = Object
>Value = MemberVariable
>
>This means an object can be decomposed to :
>
>CObject
>std::string Name
>std::map<std::string, std::string> KeyValue;
>
>Write a simple XML parser to find the elements. Then use a >factory to create the object. Assign the key-value pair to the >object.
>
>I then use a simple tree class to manage the shape of the data. >Best part of this is that it's extremely simple to turn a in >game tree of objects back in to xml.

This sounds ideal.
Can you please explain the key-value pair ,and elaborate on the
factory class?

Thank you

Jim Williams
Isn't that pretty much a poor man's DOM? TinyXml will generate a more robust DOM for you automatically. I also modified the TinyXml back-end do to a pull-parse instead of wasting 200MB of memory on the DOM when I'll only ever need to read the file in-order.
Assassin, aka RedBeard. andyc.org
Quote:Original post by Jim Williams
This sounds ideal.
Can you please explain the key-value pair ,and elaborate on the
factory class?

This is a good introduction to creating an object factory. Once the object factory creates the proper class, it can handle reading in the various attributes from the XML file.


- Houdini
- Houdini
I actually did it backwards from what you are looking at.
I did some testing with TinyXML, I had my c++ classes create their own nodes inside TinyXML, and did my management that way.
When it was time to save, I just told TinyXML to write out the XML data in memory, when I started the program I loaded the file in memory, as the C++ objects got created they found their XML node and attached to it via a pointer.

It worked really well actually.
Hi,

Well, I don't know if you've already got a solution that suits you, but if not...

I read in the C/C++ Users' Journal today about this library called XiMoL. Basically, it implements a standard STL io stream, called xstream, which makes reading and writing XML really easy, and allows you to seriallize your objects nicely.

By itself, it's not increadibly flexible, and if you want to totally decouple object representation and serialization, you still have to jump a couple hurdles, but I think this could be helpful.

Vovan
Vovan

This topic is closed to new replies.

Advertisement