c++ - 使用 QDomDocument 类的 xml 文件中的 Lastchild

标签 c++ xml qt qdomdocument

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

最佳答案

这对我有用:

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 标签。

关于c++ - 使用 QDomDocument 类的 xml 文件中的 Lastchild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633332/

相关文章:

java - 元素 eElement = (元素) nNode;应用程序崩溃

ruby - 遍历每个 xml 节点

c++ - 用3D+2D图形重绘QGLWidget

c++ - 如何实现double的序列化和反序列化?

c++ - 创建 std::pair<A,B> 时出现“operator<”错误

c++ - 在 C++ 中,如何确定一个类是否是继承链中的最后一个类/子类?即在基类的另一端

c++ - 使用原始套接字 (c++)

java - 在java中检测XML 'schema'

c++ - 防止将一个 Sprite 移动到另一个 Sprite 上

C++ 风格的 vector - 指针数组还是数组?