PDA

View Full Version : Why are only some XML nodes (found by elementsByTagName() ) removed in QDomDocument?



richardander
13th April 2010, 09:23
Hello,

I am trying to open an existing XML file, remove some nodes ( or branches ) and save the result as a new XML file. following is the code. In the original XML file, there are one "WindowLevelMap" node and many "ImageGroup" and "ImageVector" nodes. With the following code, in the new file, "WindowLevelMap" node is removed. However, only some "ImageGroup" nodes and "ImageVector" nodes are removed.

What is the problem?

thank you in advance!




...
// Create DOM;
QDomDocument doc("testDoc");
QFile orgFile(strOrignalXMLFile);
if (!orgFile.open(QIODevice::ReadWrite))
return Command::Failure;
if (!doc.setContent(&orgFile))
{
orgFile.close();
return Command::Failure;
}

// Remove useless nodes;
QDomNodeList toRemoveNodeList;
int idx;

// --
toRemoveNodeList = doc.elementsByTagName( QString("WindowLevelMap"));
for( idx = 0; idx < toRemoveNodeList.size(); idx++)
{
QDomNode parentNode = toRemoveNodeList.at(idx).parentNode();
parentNode.removeChild( toRemoveNodeList.at(idx) );
}

// --
toRemoveNodeList = doc.elementsByTagName( QString("ImageGroup"));
for( idx = 0; idx < toRemoveNodeList.size(); idx++)
{
QDomNode parentNode = toRemoveNodeList.at(idx).parentNode();
parentNode.removeChild( toRemoveNodeList.at(idx) );
}

// --
toRemoveNodeList = doc.elementsByTagName( QString("ImageVector"));
for( idx = 0; idx < toRemoveNodeList.size(); idx++)
{
QDomNode parentNode = toRemoveNodeList.at(idx).parentNode();
parentNode.removeChild( toRemoveNodeList.at(idx) );
}


// Save result;
const int Indent = 4;
QFile filteredFile(strFilteredXMLFile);
if (!filteredFile.open(QIODevice::WriteOnly))
return Command::Failure;

QTextStream out(&filteredFile);
doc.save(out, Indent);
filteredFile.close();

...

richardander
13th April 2010, 18:23
Can anyone help!

Thank you!

slendervip
22nd February 2011, 14:58
Did you get a solution to this problem?

I seem to be stuck with this problem too.