0

我正在尝试使用 TinyXML 实现一个短转换器,它将获取一个 XML 文件(具有固定格式),解析它,并用元素填充一个 protobuf 对象。问题是,protobuf 定义中的某些元素是可选的,而 TinyXML 没有模式支持。

考虑到必需/可选的差异,什么是稳健地解析元素的简单方法。我应该将所有元素粘贴到 astd::map然后检查吗?

一个示例 XML 将是

<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>

带有相应的原型定义

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;  
}
4

1 回答 1

1

看起来IsInitialized()orCheckInitialized()方法会告诉您是否已设置所有必填字段。

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

于 2010-10-17T20:48:58.990 回答