Hello there forum,

After a pretty thorough C++ course I've tried my hands at coding an actual project but have stumbled on a pretty big problem and here I am asking for your help.

What I'm trying to make right now is a class which communicates with a gameserver through a slightly modified XML-RPC and the problem arises when trying to handle the incoming responses.
These responses are nicely structured in XML format so getting the actual value isn't too hard but storing it in a more elegant way after receiving the response is.

A response can look like this:

<?xml version="1.0"?>
<methodResponse>
   <params>
      <param>
         <value><string>South Dakota</string></value>
         </param>
      </params>
   </methodResponse>

Where the value can be one of 4 simple values: int, double, string, boolean
or a struct or array like:

Array:

<array>
   <data>
      <value><i4>12</i4></value>
      <value><string>Egypt</string></value>
      <value><boolean>0</boolean></value>
      <value><i4>-31</i4></value>
      </data>
   </array>

Struct:

<struct>
   <member>
      <name>lowerBound</name>
      <value><i4>18</i4></value>
      </member>
   <member>
      <name>upperBound</name>
      <value><i4>139</i4></value>
      </member>
   </struct>

Wherein each <value> can be another array or struct.

Now what I'd like to do is read the complete response, parse it and return an object for which it's easy (intuitive) to retrieve a certain value from the response.

So far the best thing I've come up with it is saving all the actual values (that is int, string, double, boolean) in a vector in the class and returning only the structure which references to the vector. However I haven't been able to think of a proper way to store this 'structure'.


So my question to you comes down to this:
Do you know a good way to parse the XML response such that all end-values (int, string, double, boolean) are intuitively accessible while keeping the ability to nest array's and struct's?

Cheers

So my question to you comes down to this:
Do you know a good way to parse the XML response such that all end-values (int, string, double, boolean) are intuitively accessible while keeping the ability to nest array's and struct's?

it depends on your application.How you going to iterate/search with the data.
if you searching using a key value , I recommend you to use a hash-map.
other than vectors there are other data structures in STD.

Linked Lists - Does not provide random access, but faster delete and insert.
Vectors - Provides fast random access,but the worst case complexity of a
inserting or deleting a element is worse.
Tree based map- Provides Log(n) worst case complexity when searching.


Each and every data-structure have it's advantages and disadvantages.
You better analyze your input queries. Then you can definitely have an idea
about the following two.
1. what field should I use as a key.
2. what type of data-structure I should use? std::vector , std::list , ... etc.

I have done a part of my sem4 project using std::vector, but it requires a random
access , search quries takes hell of a time to complete,killing all the applications
responsiveness.

it depends on your application.How you going to iterate/search with the data.
if you searching using a key value , I recommend you to use a hash-map.
other than vectors there are other data structures in STD.

Linked Lists - Does not provide random access, but faster delete and insert.
Vectors - Provides fast random access,but the worst case complexity of a
inserting or deleting a element is worse.
Tree based map- Provides Log(n) worst case complexity when searching.


Each and every data-structure have it's advantages and disadvantages.
You better analyze your input queries. Then you can definitely have an idea
about the following two.
1. what field should I use as a key.
2. what type of data-structure I should use? std::vector , std::list , ... etc.

I have done a part of my sem4 project using std::vector, but it requires a random
access , search quries takes hell of a time to complete,killing all the applications
responsiveness.

Thanks for showing interest in such a big post.


I'll try to explain a little more carefully what I want to do with the stored data:

» The stored data doesn't have to be modified in any way.
» Once a response has been received and parsed I want to return an object from which it's intuitive to access the stored values. Here's two examples:

<array>
   <data>
      <value><i4>12</i4></value>
      <value><string>Egypt</string></value>
      <value><boolean>0</boolean></value>
      <value><i4>-31</i4></value>
      </data>
   </array>
<struct>
   <member>
      <name>lowerBound</name>
      <value><i4>18</i4></value>
      </member>
   <member>
      <name>upperBound</name>
      <value><i4>139</i4></value>
      </member>
   </struct>

Intuitive would be to be able to do something like response[0] and get an int with value 12 or response and get an int with value 18.

The problem arises solely because of the possibility (requirement) of nesting array's and struct's:

<struct>
   <member>
      <name>lowerBound</name>
      <value>
         <array>
            <data>
               <value><i4>25</i4></value>
               <value><i4>130</i4></value>
               </data>
            </array>
         </value>
      </member>
   <member>
      <name>upperBound</name>
      <value><i4>139</i4></value>
      </member>
   </struct>

Now what would seem intuitive would be response[0] and receive an int with value 25 but I haven't been able to figure out how to get this done properly yet.

However, thanks for your suggestions, and I'll take a look at hash maps first.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.