C++ Qt-XML重复标记
我有一个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
致:
我尝试了以下方法:
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;i qDebug()此行存在问题:
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;
}
}
}