c++ - Deleting specific content of XML file -


in xml file have node subchilder have 2 attributes, have delete 1 whole subchild while considering 1 attribute. have given 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 delete whole subchild "name" attribute, because value can changed.

my code until now:

void::mainwindow::xml() {     qstring path = ui->lineedit_7->text();      qdebug()<<path;     if(!file.exists() )         {         qdebug() << "check 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(); // name attributes. 

last qdebug gives me "name" attributes. stuck @ deleting subchild using 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 delete "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 open file reading." );             }               qdomdocument dom;             if( !dom.setcontent( &infile ) )             {                 qdebug( "failed parse file 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 open file writing." );             }               qtextstream stream( &outfile );             stream << dom.tostring();             outfile.close();

qdomnode has method removechild may help?

from doc

qdomnode qdomnode::removechild(const qdomnode & oldchild) 

removes oldchild list of children. oldchild must direct child of node. returns new reference oldchild on success or null node on failure.

addition code may looks follow

if( node.attributes().nameditem("name").nodevalue().compare("abc3") == 0  ) {     qdomnode parentnode = node.parentnode();     parentnode.removechild(node); } 

addition edit 2

if( node.nodename().compare("sub") == 0  ) {     qdomnode parentnode = node.parentnode();     parentnode.removechild(node); } 

update edit 3. replace 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 count of elements decreased. , don't forget close file ("file") infile.close() before call outfile.open()


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -