首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QXmlStreamReader计数条目

QXmlStreamReader计数条目
EN

Stack Overflow用户
提问于 2012-03-20 06:37:29
回答 2查看 1K关注 0票数 0

如何计算xml文件中的父项(或条目)?

下面的XML应该显示我不知道其中的条目,所以我需要计算标签的数量,以便在循环中使用它们,并将它们保存在QVector中。从那时起,我可以在xml中搜索(现在已知的)标记,并获得childs和属性(但这是另一回事)。第一步是计算父母的数量。

如果有人有更好的解决方案,请让我知道!

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<Content>
    <person1>
        <firstname>John</firstname>
        <surname>Doe</surname>
        <email>john.doe@example.com</email>
        <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person1>
    <person2>
        <various>Some Text here.</various
        <website>http://www.google.com</website>
    </person2>
    <person3>
        <firstname>Jane</firstname>
        <surname>Doe</surname>
        <email>jane.doe@example.com</email>
        <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person3>
    <person4>
        <firstname>Matti</firstname>
        <surname>Meikäläinen</surname>
        <email>matti.meikalainen@example.com</email>
        <website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
    </person4>
    <person5 Attribute="Test">
    </person5>
</Content>

我试着打开文件并逐行读取它,同时搜索一个RegExp (不太成功),就像在bash中那样。但是肯定有一种更好的解决方案,可以使用Qt4的XML函数。有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-24 06:41:02

我找到了一个解决方案。它不是很好,但它是有效的:

代码语言:javascript
复制
    QFile* file = new QFile("SomeFile.xml");
    QXmlStreamReader xml(file);

    QString ElementName = "Content";
    QString ElementName_2 = "Content";
    int i = 0;
    int k = 0;

    while( !xml.atEnd() && !xml.hasError() )
    {
        QXmlStreamReader::TokenType token = xml.readNext();

        if( token == QXmlStreamReader::StartElement )
        {
            if( xml.name().toString() != ElementName )
            {
                ElementName = xml.name().toAscii();
            }
            if( xml.name().toString() == ElementName_2 )
            {
                ElementName = xml.name().toAscii();
            }
        }
        if( token == QXmlStreamReader::EndElement )
        {
            if( xml.name().toString() == ElementName )
            {
                i++;
            }
            if( xml.name().toString() != ElementName_2 )
            {
                k++;
            }
        }
    }

    if(xml.hasError())
    {
        SCC_MsgBox(QString("%1 parsing error").arg(XMLname), "", xml.errorString(), "", 2, 3); // my own MsgBox implementation ...
        return 0;
    }
    else
    {
        i = k - i;
        return i;
    }

    xml.clear();

这个函数计算所有元素(k)和所有Childs (i),并从元素中减去Childs。所以我得到了条目的数量(在本例中是人员)。如果有人有更好的解决方案,请把它贴出来。

票数 0
EN

Stack Overflow用户

发布于 2012-03-20 20:33:41

看一下这个:http://www.digitalfanatics.org/projects/qt_tutorial/chapter09.html我更喜欢这个例子:http://jingfenghanmax.blogspot.de/2009/10/simple-qt-sax-parser.html

对于每个起始的person#-tag,我将检索该person的整个数据集,以便您在向量中推送此对象。最后,在解析XML之后,您只需要使用myVector.size();

这为我解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9778972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档