user5820174
user5820174

Reputation: 181

Deleting specific content of XML file

In my xml file i have a node whose subchilder have 2 attributes, i have to delete 1 whole subchild while considering only 1 attribute. I have given an example below

XML file:

<UMG>
  <ABC Name="ABC" Value="1"></ABC>
  <ABC Name="ABC1" Value="2"></ABC>
  <ABC Name="ABC2" Value="3"></ABC>
  <ABC Name="ABC3" Value="4"></ABC>
  <ABC Name="ABC4" Value="5"></ABC>
</UMG>
 

I have to delete the whole subchild with only "Name" attribute, because Value can be changed.

My code until now:

void::MainWindow::XML()
{
    QString path = ui->lineEdit_7->text();

    qDebug()<<path;
    if(!file.exists() )
        {
        qDebug() << "Check your file";
    }
    QDomDocument dom;
    dom.setContent(&file);
    QDomNodeList nodes = dom.elementsByTagName("ABC");

    QDomNodeList loc_childNodes = nodes.at(0).childNodes();

    for(int i=0; i<loc_childNodes.count(); i++)
    {
        QDomNode node = loc_childNodes.at(i);
        qDebug() << node.attributes().namedItem("Name").nodeValue(); // I get all Name attributes.

last qDebug gives me all "Name" attributes. I am stuck at deleting the subchild with using this information.

Edit:

<NEW>
  <child>ABC<child>
  <child1>ABC1<child1>
  <Child3>ABC3<child3>
<NEW>

EDIT2:

<MAIN>
  <SUB Name = "ABC" Value = "1"/>
  <SUB Name = "ABC1" Value = "0"/>
  <SUB Name = "ABC2" Value = "3"/>
  <Header Name = "Abc" value = "9"/>
  <SUB Name = "ABC7" Value = "3"/>
  <Header Name = "Abc5" value = "9"/>
  <SUB Name = "ABC3" Value = "3"/>
  <Header Name = "Abc0" value = "9"/>
</MAIN>

I want to delete only "SUB" attributes child. EXPECTED Result:

<MAIN>
   <Header Name = "Abc" value = "9"/>
   <Header Name = "Abc5" value = "9"/>
   <Header Name = "Abc0" value = "9"/>
</MAIN>

EDit3:

qDebug()<<manualoutput_scr;
    QString path = "File"
       QFile inFile(path );
           if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
           {
               qDebug( "Failed to open file for reading." );
           }

           QDomDocument dom;
           if( !dom.setContent( &inFile ) )
           {
               qDebug( "Failed to parse the file into a DOM tree." );
           }

           QDomElement docElem = dom.documentElement();
           QDomNodeList nodes = docElem.elementsByTagName("MAIN");
           QDomNodeList loc_childNodes = nodes.at(0).childNodes();
           for(int i=0; i<loc_childNodes.count(); i++)
              {
              QDomNode node = loc_childNodes.at(i);
              if( node.nodeName().compare("SUB") == 0  ) {
                  QDomNode parentNode = node.parentNode();
                  parentNode.removeChild(node);
              }
              }
           QFile outFile( path);
           if( !outFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
           {
               qDebug( "Failed to open file for writing." );
           }

           QTextStream stream( &outFile );
           stream << dom.toString();
           outFile.close();

Upvotes: 0

Views: 1022

Answers (1)

Alexander Chernin
Alexander Chernin

Reputation: 444

QDomNode has method removeChild may be it help?

From doc

QDomNode QDomNode::removeChild(const QDomNode & oldChild)

Removes oldChild from the list of children. oldChild must be a direct child of this node. Returns a new reference to oldChild on success or a null node on failure.

Addition Your code may looks something like follow

if( node.attributes().namedItem("Name").nodeValue().compare("ABC3") == 0  ) {
    QDomNode parentNode = node.parentNode();
    parentNode.removeChild(node);
}

Addition to Edit 2

if( node.nodeName().compare("SUB") == 0  ) {
    QDomNode parentNode = node.parentNode();
    parentNode.removeChild(node);
}

Update To Edit 3. Replace the lines

QDomElement docElem = dom.documentElement();
QDomNodeList nodes = docElem.elementsByTagName("MAIN");

with

QDomNodeList nodes = dom.elementsByTagName("MAIN");

and after parent.removeChild(node) add i-=1, because a count of elements is decreased. And don't forget to close the file ("File") inFile.close() before to call outFile.open()

Upvotes: 1

Related Questions