Sign in to follow this  
Jim Williams

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
>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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this