QXmlStreamReader读取空文本,文档肯定不为空

时间:2015-11-10 12:11:34

标签: c++ xml qt

我正在使用QXmlStreamReaderQXmlStreamWriter来读取和编写应用程序中的XML文件。

我的示例XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Senders>
    <Sender>
        <Name>COMPANY XYZ</Name>
        <Street>Random</Street>
        <BuildingNumber>23D</BuildingNumber>
        <LocalNumber>123</LocalNumber>
        <CityCode>3434-21</CityCode>
        <City>New York</City>
    </Sender>
</Senders>

它是用我的代码编写的。基本上,我没有编写XML文件的问题,但在编写后尝试阅读它们时遇到了一些麻烦。这是我的代码,它打印出空值(XML文件不为空)。

#include "mainwindow.h"
#include <QApplication>
#include <QtCore>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QFile file("../data.xml");

    if(file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QXmlStreamWriter xmlWriter(&file);
        xmlWriter.setAutoFormatting(true);
        xmlWriter.writeStartDocument();

        xmlWriter.writeStartElement("Senders");
        xmlWriter.writeStartElement("Sender");

        xmlWriter.writeTextElement("Name", "COMPANY XYZ");
        xmlWriter.writeTextElement("Street", "Random");
        xmlWriter.writeTextElement("BuildingNumber", "23D");
        xmlWriter.writeTextElement("LocalNumber", "123");
        xmlWriter.writeTextElement("CityCode", "3434-21");
        xmlWriter.writeTextElement("City", "New York");

        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();

        file.close();
    }

    QVector<QString> data;

    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QXmlStreamReader xmlReader(&file);

        while (!xmlReader.atEnd() && !xmlReader.hasError())
        {
            xmlReader.readNext();
            if (xmlReader.isStartElement())
            {
                qDebug() << "element name: '" << xmlReader.name().toString() << "'"
                         << ", text: '" << xmlReader.text().toString() << "'" << endl;
            }
        }
        file.close();
    }

    return a.exec();
}

那么在阅读我的文件时我做错了什么?

我得到的输出:

element name: ' "Senders" ' , text: ' "" ' 

element name: ' "Sender" ' , text: ' "" ' 

element name: ' "Name" ' , text: ' "" ' 

element name: ' "Street" ' , text: ' "" ' 

element name: ' "BuildingNumber" ' , text: ' "" ' 

element name: ' "LocalNumber" ' , text: ' "" ' 

element name: ' "CityCode" ' , text: ' "" ' 

element name: ' "City" ' , text: ' "" ' 

1 个答案:

答案 0 :(得分:3)

当您处于StartElement令牌时,您只能获取此元素的名称,而不是它的值。您需要进一步阅读并获取Characters令牌以便能够读取元素的值。你还想跳过只有空白的标记:

while (!xmlReader.atEnd() && !xmlReader.hasError())
{
    xmlReader.readNext();
    //here we are at StartElement, so we can read the element's name
    if (xmlReader.isStartElement())
    {
        qDebug() << "element name: '" << xmlReader.name().toString() << "'";
    }
    //here we are inside the element, so if it is not empty, we can read the element's value
    else if(xmlReader.isCharacters() && !xmlReader.isWhitespace())
    {
        qDebug() << "element value: '" <<xmlReader.text().toString() << "'";
    }
}