要求 TinyXML 中存在没有模式的 XML 元素

发布于 2024-09-27 08:35:21 字数 1224 浏览 0 评论 0原文

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

考虑到必需/可选差异,稳健地解析元素的简单方法是什么?我应该将所有元素粘贴到 std::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;  
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2024-10-04 08:35:21

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文