c++ - Lastchild in xml file using QDomDocument Class -


i have xml:

<vcaanalysis>     <vcastream>         <vcaframe width="768" height="432" rtptime=""    utctime="102157000" utctimehigh="0" configid="0" />         <vcaframe width="768" height="432" rtptime="" utctime="102157160" utctimehigh="0" configid="0">             <object objectid="138.96.200.59_20160126_102157160_1" minx="276" miny="0" maxx="320" maxy="123" width="44" height="123" objproptag="person">             </object>         </vcaframe>         <vcaframe width="768" height="432" rtptime="" utctime="102157320" utctimehigh="0" configid="0" />         <vcaframe width="768" height="432" rtptime="" utctime="102157480" utctimehigh="0" configid="0">             <object objectid="138.96.200.59_20160126_102157480_2" minx="224" miny="264" maxx="287" maxy="343" width="63" height="79" objproptag="person">             </object>         </vcaframe>         <vcaframe width="768" height="432" rtptime="" utctime="102157640" utctimehigh="0" configid="0">             <object objectid="138.96.200.59_20160126_102157480_3" minx="204" miny="266" maxx="331" maxy="400" width="127" height="134" objproptag="person">             </object>         </vcaframe>        <vcaframe width="768" height="432" rtptime=""    utctime="102157000" utctimehigh="0" configid="0" />      </vcastream>   </vcaanalysis> 

i want last objectid(138.96.200.59_20160126_102157480_3) in last vcaframe have object.

i tried code doesn't work.

         qdomnodelist = vcastream.elementsbytagname("vcaframe");          if(a.size()!=0) {           qdomelement lastobj = vcastream.lastchild().toelement();          qdomelement last = lastobj.firstchild().toelement();           qstring lastid = last.attribute("objectid");          cout << qprintable("laaaaaaaast "+lastid) << endl;         } 

this worked me:

qdomnodelist vcastreams = vcastream.elementsbytagname("vcastream"); qdomnodelist vcaframes = vcastreams.at(0).childnodes(); //gives 6 vcaframe tags qdomnodelist vcaobjects = vcaframes.at(4).childnodes(); //gives 1 object tag qdebug() << vcaobjects.at(0).toelement().attribute("objectid");  

lastobj in code refers last vcaframe, not have objectid.

edit: if need iterate on entire xml file. i'm assuming want last vcaframe has objectid in each vcastream.

qdomnodelist vcastreams = vcastream.elementsbytagname("vcastream");  (int = 0; < vcastreams.count(); ++i) {     qdomnodelist vcaframes = vcastreams.at(i).childnodes(); //gives vcaframetags      //find last tag objectid     qdomelement last;     (int j = vcaframes.count() - 1; j >= 0; --j) {         //assumes there @ 1 <object> tag in each vcaframe         if (vcaframes.at(j).haschildnodes()) {             qdomelement tmp = vcaframes.at(j).firstchild().toelement();             if (tmp.hasattribute("objectid")) {                 last = tmp;                 break;             }         }     }      //last holds last vcaframe object tag or null     if (last.isnull())         qdebug() << "no objectid found";     else          qdebug() << last.attribute("objectid");  } 

i tested on xml file , gave me correct result, did not try adding more 1 vcastream tag.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -