How to initialize a C++ object from XML

Discussion in 'XML' started by BCC, Feb 22, 2005.

  1. BCC

    BCC Guest

    I have some c++ code like this:
    class MyObject {
    public:
    int m_a;
    int m_b;
    int m_c;
    // etc. etc.
    }

    And in an XML file I have something like this:
    <variable initialization>
    <object id="1">
    <a>10</a>
    <b>3 </a>
    <m>332</m>
    </object>
    <object id="2">
    <a>15</a>
    <b>5 </a>
    <m>110</m>
    </object>
    </variable initialization>

    Where basically I have a list of objects, and I need to create that number
    (in this case 2) of objects, and initialize their member variables
    accordingly.

    What is a nice way to do this? In the xml file, I may only have 1 or 2 of
    the objects variables (and there can be a lot of these) or I may have all of
    them.

    It seems the obvious but painful way would be when I get my xml document
    loaded, to go through the thing and search for every variable in my object
    to see if it has been set in the xml file. I thought about a hash map, but
    that still doesnt sound nice.

    I would guess people have to do this all the time, any suggestions besides a
    load of if/then statements?

    Im using MSXML by the way...

    Thanks,
    Bryan
     
    BCC, Feb 22, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page