0

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();

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

11 Comments

How do i define a child, i did try with " QDomElement cild = node.attributes().namedItem("Name").nodeValue() == "ABC3"
if it works, why not? Then call method parentNode and remove the childNode. Something like this parent = childNode.parentNode(); parent.removeChild(childNode);
I cannot declare the child " QDomElement cild = node.attributes().namedItem("Name").nodeValue() == "ABC3", I keep getting ": error: C2440: 'initializing' : cannot convert from 'bool' to 'QDomElement' No constructor could take the source type, or constructor overload resolution was ambiguous"
if( node.attributes().namedItem("Name").nodeValue().compare("ABC3") == 0 ) { QDomNode parentNode = node.parentNode(); parentNode.removeChild(node); } and don't forget rtfm :)
Thanks! Work! Can you tell me how do i delete a node without any attribute, i have mentioned a examaple in my main post
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.