【问题标题】:Reading xml file using QDomDocument just get the first line使用 QDomDocument 读取 xml 文件只需获取第一行
【发布时间】:2012-11-13 13:01:13
【问题描述】:

我使用 QXmlStreamWriter 生成一个 xml 文件。该文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<pedestrianinfo>
    <pedestrian uuid="2112e2ed-fc9b-41e8-bbcb-b44ad78bde11">
        <module>11.1208</module>
        <direction>4</direction>
        <row>5</row>
        <column>71</column>
    </pedestrian>
    <pedestrian uuid="1aabb9c1-4aa7-4f47-9542-36d2dfaa26e4">
        <module>1.48032</module>
        <direction>4</direction>
        <row>67</row>
        <column>31</column>
    </pedestrian>

...

</pedestrianinfo>

然后我尝试通过 QDomDocument 读取内容。我的代码如下所示:

xmlReader *xp = new xmlReader(QString("D:\\0T.xml"));
    if(xp->openFile()) {
        if(xp->isGetRootIndex()) {
            xp->parseRootIndexElement();
        }
        else
            cout<<"Unable to get root index."<<endl;
    }

这里是isGetRootIndex():

bool xmlReader::isGetRootIndex()
{
    doc.setContent(&file,false);
    root = doc.documentElement();
    if(root.tagName() == getRootIndex()) //rootIndex=="pedestrianinfo"
        return true;
    return false;
}

这是 parseRootIndexElement():

void xmlReader::parseRootIndexElement()
{
    QDomNode child = root.firstChild();
    while(!child.isNull()) {
        if(child.toElement().tagName() == getTagNameP())  //"childTagName=="pedestrian"
            parseEntryElement(child.toElement());
        qDebug()<<"module="<<module<<" direction="<<direction<<" row="<<row<<" column="<<column;
        child = child.nextSibling();
    }
}

parseEntryElement(const QDomElement &element) 是一个函数,用于获取每个标签中的信息并将它们保存到模块等变量中。 但是,每次我运行我的代码时,只有 xml 文件的第一个子文件可能是 qDebug*ed*。似乎在执行 child.nextSibling() 之后,child 变为 null。为什么没有得到下一个行人信息?

【问题讨论】:

    标签: c++ xml qt


    【解决方案1】:

    根据我在文档中看到的内容,我看起来是正确的。也许 parseEntryElement 意外地推进了迭代器?

    【讨论】:

    • 感谢您的回复。今天我发现我犯了一个可怕的错误。该程序读取“旧”xml 文件而不是正确的。哎呀。该代码现在对我有用。
    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多