我正在尝试使用C ++中的Boost Ptrees做一些简单的xml解析。但是,似乎该read_xml
函数仅在不存在结束标记的情况下才会引发错误。下面抛出一个错误。例如:
<?xml version="1.0" encoding="utf-8"?>
<Grandparent>
<Parent>test<Parent>
</Grandparent>
请注意,Parent的结束标记没有斜杠正斜杠,并且将其作为错误抛出。即使缺少结束标记之类的东西<Parent>test
也会引发预期的有效错误。
但是,如果结束标记字符串与开始标记字符串不匹配,则不会引发错误。例如:
<?xml version="1.0" encoding="utf-8"?>
<Grandparent>
<Parent>test</Child>
</Grandparent>
上面的解析就很好了。我的代码非常简单,如下所示:
using boost::property_tree::ptree;
ptree pt;
read_xml(xmlpath, pt);
我在这里有什么事要注意吗?
是。
最重要的是:Boost属性树不是XML库。
其次,引擎盖下使用的Rapidxml实现具有关闭标签验证作为选择加入:
if (Flags & parse_validate_closing_tags)
{
// Skip and validate closing tag name
Ch *closing_name = text;
skip<node_name_pred, Flags>(text);
if (!internal::compare(node->name(), node->name_size(), closing_name, text - closing_name, true))
BOOST_PROPERTY_TREE_RAPIDXML_PARSE_ERROR("invalid closing tag name", text);
}
幸运的是,Boost Property没有选择加入。实际上,它不允许您选择:
/// Text elements should be put in separate keys,
/// not concatenated in parent data.
static const int no_concat_text = 0x1;
/// Comments should be omitted.
static const int no_comments = 0x2;
/// Whitespace should be collapsed and trimmed.
static const int trim_whitespace = 0x4;
inline bool validate_flags(int flags)
{
return (flags & ~(no_concat_text | no_comments | trim_whitespace)) == 0;
}
不允许其他标志。
如果您需要XML解析,建议您转向XML库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句