要求 TinyXML 中存在没有模式的 XML 元素
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来
IsInitialized()
或CheckInitialized()
方法会告诉您是否已设置所有必填字段。http://code.google .com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.message.html#Message.IsInitialized
Looks like the
IsInitialized()
orCheckInitialized()
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