QXMLStreamReader正在跳过文本元素

时间:2019-01-31 17:55:08

标签: c++ xml qt

我正在写一些数据的XML文件。稍后,我想读取XML文件并在程序中使用数据。但是,当我重新阅读它时,QXMLStreamReader似乎跳过了一些子元素。

这是我的XML文件的一个片段:

<?xml version="1.0" encoding="UTF-8"?>
<TEMPROOT>
    <Parent1>
        <C0>238.195|1401.12</C0>
        <C1>795.475|1087.65</C1>
        <C2>995.748|756.766</C2>
    </Parent1>
    <Parent2>
        <left>248</left>
        <right>671</right>
        <Width>496</Width>
        <Height>583</Height>
    </Parent2>
    <Parent3>
        <Number>9</Number>
        <Blue>4</Blue>
        <Red>5</Red>
    </Parent3>
</TEMPROOT>

它正确读取Parent1及其子级。我的代码可以识别Parent2,但是会跳过它的子代,而直接进入Parent3,它会正确读取其子代。

这是我的代码,其中我了XML:

QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("TEMPROOT");

xmlWriter.writeStartElement("Parent1");
xmlWriter.writeTextElement("C0", str0);
xmlWriter.writeTextElement("C1", str1);
xmlWriter.writeTextElement("C2", str2);
xmlWriter.writeEndElement();

xmlWriter.writeStartElement("Parent2");
xmlWriter.writeTextElement("left", QString::number(rectCoord[0]));
xmlWriter.writeTextElement("right", QString::number(rectCoord[1]));
xmlWriter.writeTextElement("Width", QString::number(rectCoord[2]));
xmlWriter.writeTextElement("Height", QString::number(rectCoord[3]));
xmlWriter.writeEndElement();

xmlWriter.writeStartElement("Parent3");
xmlWriter.writeTextElement("Number", strNum);
xmlWriter.writeTextElement("Blue", strBlue);
xmlWriter.writeTextElement("Red", strRed);
xmlWriter.writeEndElement();

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

这是我在XML文件中读取的方式:

QXmlStreamReader xmlReader(&file);
QVector<QString> Qv1;
QVector<double> QV2;
QVector<QString> Qv3;
while (!xmlReader.isEndDocument())
{
    xmlReader.readNext();
    QString Name = xmlReader.name().toString();
    if (Name == "Parent1")
    {
        while (xmlReader.name().toString() != "Parent2")
        {
            if (xmlReader.name().toString().at(0) == "C")
            {
                QV1.append(xmlReader.readElementText());
            }
            xmlReader.readNext();
        }
     }
     else if (Name == "Parent2")
    {
        while (xmlReader.name().toString() != "Parent3")
        {
            if (xmlReader.name().toString() == "left")
            {
                QV2.append(xmlReader.readElementText().toDouble());
            }
            else if (xmlReader.name().toString() == "right")
            {
                QV2.append(xmlReader.readElementText().toDouble());
            }
            else if (xmlReader.name().toString() == "Width")
            {
                QV2.append(xmlReader.readElementText().toDouble());
            }
            else if (xmlReader.name().toString() == "Height")
            {
                QV2.append(xmlReader.readElementText().toDouble());
            }
            xmlReader.readNext();

        }
    }
    else if (Name == "Number")
    {
        Qv3.append(xmlReader.readElementText());
    }
    else if (Name == "Blue")
    {
        Qv3.append(xmlReader.readElementText());
    }
    else if (Name == "Red")
    {
        Qv3.append(xmlReader.readElementText());
    }
}

主要问题是Name变量(它是xmlReader上当前XML元素的名称,从Parent2直变为{{1 }}忽略Parent3的子级。 Parent2Parent1的子级被读入没有问题。读取或写入xml时,我做错什么了吗?

0 个答案:

没有答案