2016-07-28 5 views
1

Ich habe dieses xml:Lastchild in xml-Datei mit QDomDocument Klasse

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

Ich mag die letzte objectID (138.96.200.59_20160126_102157480_3) in dem letzten VCAFrame erhalten, die ein Objekt hat.

Ich habe diesen Code versucht, aber es funktioniert nicht.

  QDomNodeList a = 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; 
     } 
+0

versuchen, durch zu ersetzen: QDomNodeList a = VCAStream.elementsByTagName ("VCAStream"); – Mimouni

+0

es ist das gleiche Ergebnis –

Antwort

1

Das ist für mich gearbeitet:

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 im Code zur letzten VCAFrame bezieht, die keine objectID hat.

EDIT: Wenn Sie über eine vollständige XML-Datei iterieren müssen. Ich nehme an, dass Sie den letzten vcaFrame wollen, der eine objectID in jedem VCAStream hat.

QDomNodeList vcaStreams = VCAStream.elementsByTagName("VCAStream"); 

for (int i = 0; i < vcaStreams.count(); ++i) { 
    QDomNodeList vcaFrames = vcaStreams.at(i).childNodes(); //Gives us all VCAFrameTags 

    //Find last tag with objectID 
    QDomElement last; 
    for (int j = vcaFrames.count() - 1; j >= 0; --j) { 
     //Assumes there is at most one <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 now holds the last VCAFrame with an object tag or is Null 
    if (last.isNull()) 
     qDebug() << "No objectID found"; 
    else 
     qDebug() << last.attribute("objectID"); 

} 

Getestet habe ich diese auf dem XML-Datei und es gab mir das richtige Ergebnis, aber ich habe nicht versucht, mehr als ein VCAStream Tag hinzufügen.

+0

Wenn ich eine andere XML-Datei habe, wird es nicht funktionieren. ich brauche die letzte objectID im letzten VCAFrame mit einer objectID. –

+0

Ich habe meine Antwort bearbeitet, um mit jeder XML-Datei umzugehen. Lass es mich wissen, wenn das hilft. – Dillydill123

+0

Vielen Dank es funktioniert. –