Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt-XML重复标记_C++_Qt_Qt5 - Fatal编程技术网

C++ Qt-XML重复标记

C++ Qt-XML重复标记,c++,qt,qt5,C++,Qt,Qt5,我有一个xml文件,只想复制一些特定节点: 来自(示例): 致: 我尝试了以下方法: for(int i = 0; i < xmlRoot.childNodes().count(); i++) { if(xmlRoot.childNodes().at(i).isElement()){ if(xmlRoot.childNodes().at(i).toElement().attribute("id") == "team

我有一个xml文件,只想复制一些特定节点:

来自(示例):


致:


我尝试了以下方法:

    for(int i = 0; i < xmlRoot.childNodes().count(); i++)    {
    if(xmlRoot.childNodes().at(i).isElement()){
        if(xmlRoot.childNodes().at(i).toElement().attribute("id") == "teamSection"){ //find goal element
            teamNode = xmlRoot.childNodes().at(i).cloneNode(); //copy element

            if(xmlRoot.childNodes().at(i).insertAfter(teamNode, xmlRoot.childNodes().at(i)).isNull()){
                qDebug() << "not worked";
            }
            else{
                qDebug() << "worked";
            }
            break;
        }
    }
}
for(int i=0;iqDebug()此行存在问题:

xmlRoot.childNodes().at(i).insertAfter(teamNode, xmlRoot.childNodes().at(i))  
insertAfter
方法接受两个参数-新节点和将作为新节点插入引用的节点。但是,这两个参数都必须是调用
insertAfter
的公共父级的子级。从原理图上看,您的代码类似于
child->insertAfter(newChild,child)
而它应该是
parent->insertAfter(newChild,child)
。您可以查看下面的代码:

for (int i = 0; i < xmlRoot.childNodes().count(); i++)
{
    if (xmlRoot.childNodes().at(i).isElement())
    {
        if(xmlRoot.childNodes().at(i).toElement().attribute("id") == "teamSection")
        {
            auto teamNode = xmlRoot.childNodes().at(i).cloneNode(); //copy element
            auto sibling = xmlRoot.childNodes().at(i);

            if (xmlRoot.insertAfter(teamNode, sibling).isNull())
            {
                qDebug() << "not worked";
            }
            else
            {
                qDebug() << "worked";
            }
            break;
        }
    }
}
for(int i=0;ixmlRoot.childNodes().at(i).insertAfter(teamNode, xmlRoot.childNodes().at(i))  
for (int i = 0; i < xmlRoot.childNodes().count(); i++)
{
    if (xmlRoot.childNodes().at(i).isElement())
    {
        if(xmlRoot.childNodes().at(i).toElement().attribute("id") == "teamSection")
        {
            auto teamNode = xmlRoot.childNodes().at(i).cloneNode(); //copy element
            auto sibling = xmlRoot.childNodes().at(i);

            if (xmlRoot.insertAfter(teamNode, sibling).isNull())
            {
                qDebug() << "not worked";
            }
            else
            {
                qDebug() << "worked";
            }
            break;
        }
    }
}