Rapidxml next_sibling 不返回下一个兄弟

问题描述

我想按属性选择“实际”xml节点。

当我尝试遍历节点时,它只返回第一个, 但奇怪的是,当我询问最后一个节点时,它会毫无问题地返回它。另外,如果我用“actualroot->first_node("roodnode1")”之类的名称搜索它,它会找到它。 编辑: 我检查了一下以防万一,“actualroot”有父母

Rapidxml 版本:1.13 (我也试过不同的版本(1.1),结果一样)

保存的xml:

<rootnode Game="gamename">
    <rootnode0 table="a" cluster="b" item="c"/>
    <rootnode1 table="atest" cluster="btest" item="ctest"/>
    <rootnode2 table="1" cluster="2" item="3"/>
</rootnode>

我使用的代码的简化版:

#include "rapidxml.hpp"
#include "rapidxml_print.hpp"


using namespace std;
using namespace rapidxml;

bool listloaded;
bool actualcontainerloaded;
int rootcounter;
xml_document<> *actuallist;
xml_node<>* actualroot;
xml_node<>* actualcontainer;
std::unordered_map<std::string,xml_document<>*> xmllistmap;

main()
{
listloaded = false;
actualcontainerloaded = false;
rootcounter = 0;

create("tastx")
addcontainer("a","b","c")
addcontainer("atest","btest","ctest")
addcontainer("1","2","3")
setactualcontainer("atest","ctest")
}


bool create(string name)
{
   if (isalreadyexist(name)) return false;
   //RPXdata oneRPX;
   xml_document<> *newlist = new xml_document<>(); 
   xml_node<>* root = newlist->allocate_node(node_element,newlist->allocate_string("rootnode"));
   root->append_attribute(newlist->allocate_attribute("Game",newlist->allocate_string("gamename")));
   newlist->append_node(root);
   newlist->name(name.c_str());
   xmllistmap[name] = newlist;
   actuallist = newlist;
   actualroot = newlist->first_node();
   listloaded = true;
   return true;
}




bool addcontainer(string table,string cluster,string item)
{
    if (listloaded)
    {
        
        for (xml_node<> *child = actualroot->first_node(); child; child = actualroot->next_sibling())
        {
            
            if (child->first_attribute("table") == NULL) continue;
            string tableattr = child->first_attribute("table")->value();
            if (tableattr != table) continue;
            if (child->first_attribute("cluster") == NULL) continue;
            string clusterattr = child->first_attribute("cluster")->value();
            if (clusterattr != cluster) continue;
            if (child->first_attribute("item") == NULL) continue;
            string itemattr = child->first_attribute("item")->value();
            if (itemattr == item) continue;
            return false;
        }
        string rootname = "rootnode"+functions.converttostring(rootcounter);
        xml_node<>* root = actuallist->allocate_node(node_element,actuallist->allocate_string(rootname.c_str()));
        root->append_attribute(actuallist->allocate_attribute("table",actuallist->allocate_string(table.c_str())));
        root->append_attribute(actuallist->allocate_attribute("cluster",actuallist->allocate_string(cluster.c_str())));
        root->append_attribute(actuallist->allocate_attribute("item",actuallist->allocate_string(item.c_str())));
        actualroot->append_node(root);
        functions.log("container added:" + table);
        rootcounter++;
    }

    return false;
}


bool setactualcontainer(string table,string item)
{
    if (listloaded)
    {
        for (xml_node<> *child = actualroot->first_node(); child; child = actualroot->next_sibling())
        {
            xml_node<> *lastchild = actualroot->last_node();
            string lcname = lastchild->name();
            functions.log("last name: " + lcname);
            string cname = child->name();
            functions.log("cname: " + cname);
            functions.log("for step called");
            if (child->first_attribute("table") == NULL) continue;
            string tableattr = child->first_attribute("table")->value();
            string clusterattr = child->first_attribute("cluster")->value();
            string itemattr = child->first_attribute("item")->value();
            functions.log("actual tableattr:" + tableattr);
            
            if (tableattr == table && clusterattr == cluster && itemattr == item)
            {
                functions.log("actual continer settd:"+ table);
                actualcontainer = child;
                actualcontainerloaded = true;
                return true;
            }
            
        }
        
    }
    return false;
}

结果:

23:11:17: container added:a
23:11:17: container added:atest
23:11:17: container added:1
23:11:17: last name: rootnode2
23:11:17: cname: rootnode0
23:11:17: for step called
23:11:17: actual tableattr:a

解决方法

只是一个简单的疏忽: for循环

for (xml_node<> *child = actualroot->first_node(); child; child = actualroot->next_sibling())

实际上应该是这样的:

for (xml_node<> *child = actualroot->first_node(); child; child = child->next_sibling())

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...