View Full Version : Problem with Deleting QDomNode
metdos
15th July 2010, 08:59
Here my source code:
QDomElement rootOfTag=fieldState.firstChild().firstChildElemen t(parent);
QDomNodeList sameTags=rootOfTag.elementsByTagName(node.firstChi ld().toElement().tagName());
for(uint i=0;i<sameTags.length();i++)
{
QDomNode nodeToReplace=sameTags.item(i);
if(nodeToReplace.toElement().attribute("id")==node.firstChild().toElement().attribute("id"))
{
rootOfTag.removeChild(nodeToReplace);
return true;
}
}
Although it enters into if part, it can't remove "nodeToReplace" . What can be my problem?
Edit: nodeToReplace.nodeType() returns QDomNode::ElementNode, can problem related with that?
Lykurg
15th July 2010, 09:03
Are you sure that nodeToReplace is a direct child of rootOfTag? How does a sample XML file look like?
metdos
15th July 2010, 09:39
fieldState is QDomDocument,
parent="Blocks"
node.firstChild().toElement().tagName() returns "Block"
<root>
<Blocks>
<Block id="1"></Block>
<Block id="2"></Block>
<Block id="3"></Block>
</Blocks>
</root>
Lykurg
15th July 2010, 10:16
Hm, in the if block you have
return true; which exits your function, so how does the function definition looks like? Is fieldState a copy or a pointer so that the changes you make are propagated to your "real" QDomDocument? That's the only reason I can think of. May be you want check inside the if block if the node is deleted.
metdos
15th July 2010, 12:19
Whole Function:
bool FieldModel::updateNode(QDomNode node,QString parent){
QDomElement rootOfTag=fieldState.firstChild().firstChildElemen t(parent);
QDomNodeList sameTags=rootOfTag.elementsByTagName(node.firstChi ld().toElement().tagName());
for(uint i=0;i<sameTags.length();i++){
QDomNode nodeToReplace=sameTags.item(i);
if(nodeToReplace.toElement().attribute("id")==node.firstChild().toElement().attribute("id")){
rootOfTag.removeChild(nodeToReplace);
qDebug()<<"Hello:\n"<<fieldState.toString();
return true;
}
}
insertNode(node,parent);
return true;
}
Its output from line 9.
Hello:
"<FieldState>
<Blocks>
<Block id="3T">
<OccupiedState>1</OccupiedState>
<TanzimState>0</TanzimState>
<ProtectionState>0</ProtectionState>
<FailureState>0</FailureState>
</Block>
<Block id="001BT">
<OccupiedState>1</OccupiedState>
<TanzimState>0</TanzimState>
<ProtectionState>0</ProtectionState>
<FailureState>0</FailureState>
</Block>
</Blocks>
<Switches/>
<Signals/>
<LevelCrossings/>
<Routes/>
</FieldState>
"
fieldState is Class member and it is fine.
metdos
16th July 2010, 08:38
http://www.qtcentre.org/threads/12313-remove-node-in-xml-file
This solved my problem.
Lykurg
16th July 2010, 18:58
Are you sure that nodeToReplace is a direct child of rootOfTag?
http://www.qtcentre.org/threads/12313-remove-node-in-xml-file
This solved my problem.
You could have had it earlier...;)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.