我正在尝试使用 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 Property Tree 不是 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 库。
关于c++ - Boost ptree 读取 xml 中的结束标记 xml 验证不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804185/