【发布时间】:2018-03-14 14:06:54
【问题描述】:
我正在使用以下类型的 boost 属性树解析 XML 文件:
<DocName>
<InitCommands>
<OptionalCommands>
<command name="write" address="0x00000000"/>
<command name="write" address="0x00000000"/>
<command name="write" address="0x00000000"/>
</OptionalCommands>
<command name="write" address="0x00000000"/>
<command name="write" address="0x00000000"/>
</InitCommands>
</DocName>
我想检查某个元素是否存在于 XML 树中。以下是我正在使用的代码:
namespace pt = boost::property_tree;
int main (){
pt::ptree prop_tree;
read_xml ("my.xml", prop_tree);
pt::ptree::const_assoc_iterator it;
it = prop_tree.find("DocName.InitCommands.OptionalCommands");
if (it != prop_tree.not_found())
std::cout <<"Optional Options found !" << std::endl;
}
但是,运行此代码会返回
it == prop_tree.not_found()
如果我尝试找到我的 xml 文件的根元素,这会起作用,即
it = prop_tree.find("DocName");
有人可以建议如何使用这个 find() 函数吗?
【问题讨论】:
标签: c++ xml boost-propertytree