1

我有这个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>

我想在最后一个有对象的 VCAFrame 中获取最后一个 objectID(138.96.200.59_20160126_102157480_3)。

我试过这段代码,但它不起作用。

         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;
        }
4

1 回答 1

1

这对我有用:

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在您的代码中是指最后一个 VCAFrame,它没有 objectID。

编辑:如果您需要遍历整个 xml 文件。我假设您想要在每个 VCAStream 中具有 objectID 的最后一个 vcaFrame。

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

}

我在您的 XML 文件上对此进行了测试,它给了我正确的结果,但我没有尝试添加多个 VCAStream 标记。

于 2016-07-28T15:10:37.923 回答