views:

22

answers:

1

I am trying to implement a short converter using TinyXML that will take an XML file (with fixed format), parse it, and populate a protobuf object with the elements. Problem is, some elements are optional in the protobuf definition and TinyXML does not have schema support.

What would be a simple way to parse the elements robustly taking into account the required/optional difference. Should I stick all elements into a std::map and then check?

An example XML would be

<box>
  <id>495</bin_id>
  <region>
    <vertices>
      <x>233</x>
      <y>208</y>
    </vertices>
    <vertices>
      <x>233</x>
      <y>188</y>
    </vertices>
    <vertices>
      <x>253</x>
      <y>188</y>
    </vertices>
    <vertices>
      <x>253</x>
      <y>208</y>
    </vertices>
  </region>
  <type>Pencils</type>
  <color>GREEN</color>
  <deplete_level_thr>0.2</deplete_level_thr>
  <replenish_level_thr>0.8</replenish_level_thr>
<box>

with the corresponding proto definition

message ProduceBin {
  required int64 id = 1;            
  required system.messaging.Polygon region = 2; 
  optional string type = 3;     
  optional string color = 4;            
  optional double deplete_level_thr = 6;    
  optional double replenish_level_thr = 7;  
}
+1  A: 

Looks like the IsInitialized() or CheckInitialized() methods will tell you if all the required fields have been set.

http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.message.html#Message.IsInitialized

Joshua Martell

related questions