1

我在使用 QXmlStreamReader 获取嵌套标签的值时遇到问题。我的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<Class Name="Simple1">
    <Property Name="FloatValue" Type="Float">14.45</Property>
    <!-- komentarz -->
    <Property Name="IntegerValue" Type="Integer">-23049</Property>
    <Property Name="UnsignedValue" Type="Unsigned">123</Property>   
</Class>

<Class Name="Simple2">
    <Property Name="TestEmbed" Type="Embed" SubType="Simple1">
        <Property Name="FloatValueEmbed" Type="Float">1.2</Property>
        <Property Name="IntegerValueEmbed" Type="Integer">10</Property>
        <Property Name="UnsignedValueEmbed" Type="Unsigned">20</Property>        
    </Property>
    <Property Name="FloatValue" Type="Float">55.55</Property>
    <Property Name="IntegerValue" Type="Integer">-1111</Property>
    <Property Name="UnsignedValue" Type="Unsigned">888</Property>
</Class>
</root>

我只是无法获取位于其他属性内部的标签属性的值:

        while (reader.readNextStartElement())
        {
            qDebug() << "Found tag: " << reader.name() << "text: " << reader.text() << "token: " << reader.tokenString();// THIS PART WORKS WRONG << "tag value: " << reader.readElementText(); //tried IncludeChildElements too
            for (auto &attribute : reader.attributes())
            {
                qDebug() << "attribute name: " << attribute.name() << ", attribute value: " << attribute.value();
            }
            reader.skipCurrentElement();
        }

怎么了?类 Simple1 已正确读取和列出。

4

1 回答 1

0

自己找到了解决方案 - 如果有人会遇到类似的问题,这里是工作代码:

while (reader.readNextStartElement())
{
    qDebug() << "Found tag: " << reader.name() << "text: " << reader.text() << "token: " << reader.tokenString();
    for (auto &attribute : reader.attributes())
    {
        qDebug() << "attribute name: " << attribute.name() << ", attribute value: " << attribute.value();
    }
    reader.readNext();
    qDebug() << "tag value:" << reader.text();
    reader.skipCurrentElement();
}

进入控制台:

Found tag:  "Property" text:  "" token:  "StartElement"
attribute name:  "Name" , attribute value:  "FloatValueEmbed"
attribute name:  "Type" , attribute value:  "Float"
tag value: "1.2"
Found tag:  "Property" text:  "" token:  "StartElement"
attribute name:  "Name" , attribute value:  "IntegerValueEmbed"
attribute name:  "Type" , attribute value:  "Integer"
tag value: "10"
Found tag:  "Property" text:  "" token:  "StartElement"
attribute name:  "Name" , attribute value:  "UnsignedValueEmbed"
attribute name:  "Type" , attribute value:  "Unsigned"
tag value: "20"
于 2019-12-13T10:03:09.357 回答