防止重复的pugixml :: xml_node [英] Prevent duplicate pugixml::xml_node
问题描述
我在写我的应用程序的一部分,存储设置在XML文件,但我不想'客户端'重复,我想这样:
I'm writing a part of my app that store settings in XML file, but I don't want to 'client' duplicate, I want this:
<jack>
<client name="something">
<port name="someport" />
<port name="someport_2" />
</client>
</jack>
但我得到:
<jack>
<client name="something">
<port name="someport" />
</client>
<client name="something">
<port name="someport_2" />
</client>
</jack>
认为只是检查节点是否已经存在,但这是问题,所以我有这个代码:
thought "just check if node already exists" but that's the problem, so I've this piece of code:
// xjack is the root node
pugi::xml_node xclient = xjack.child(sclient.c_str());
if (!xclient) {
xclient = xjack.append_child("client");
}
但!xclient
总是评估为true,尝试也 if(xclient.empty())
但不工作也。
but !xclient
always evaluate to true, tried also if (xclient.empty())
but not work also.
推荐答案
考虑评论zeuxcg我可以找出错误的原因。
thinking about the comments zeuxcg I could figure out what was wrong.
pugi :: xml_node xclient = xjack.child(sclient.c_str());
正在查找名称为something ,我要找的是一个标签,名称为client,属性name的值为something。
pugi::xml_node xclient = xjack.child(sclient.c_str());
is looking up for a child with name "something" that really doesn't exists, what I'm looking for is a tag with name "client" and attribute "name" with value of "something".
p>
So, the correct is:
pugi::xml_node xclient = xjack.find_child_by_attribute("client", "name", sclient.c_str());
if (!xclient) {
xclient = xjack.append_child("client");
xclient.append_attribute("name").set_value(sclient.c_str());
}
这篇关于防止重复的pugixml :: xml_node的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!