关于C ++:在Boost ptree中读取End标记xml验证不正确xml

End tag xml validation incorrect in Boost ptree read xml

我正在尝试使用C ++中的Boost Ptrees做一些简单的xml解析。 但是,似乎read_xml函数仅在不存在结束标记的情况下才会引发错误。 下面抛出一个错误。
例如:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<Grandparent>
<Parent>test<Parent>
</Grandparent>

请注意,Parent的结束标记没有斜杠正斜杠,并且将其作为错误抛出。 即使缺少像test这样的结束标记,也会引发预期的有效错误。

但是,如果结束标记字符串与开始标记字符串不匹配,则不会引发错误。 例如:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<Grandparent>
<Parent>test</Child>
</Grandparent>

上面的解析就很好。 我的代码非常简单,如下所示:

1
2
3
using boost::property_tree::ptree;
ptree pt;
read_xml(xmlpath, pt);

我在这里有什么事要注意吗?


是。

最重要的是:Boost属性树不是XML库。

其次,引擎盖下使用的Rapidxml实现具有选择加入的结束标签验证功能:

1
2
3
4
5
6
7
8
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没有选择加入。 实际上,它不允许您选择:

1
2
3
4
5
6
7
8
9
10
11
12
/// 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库。