I have a problem with getting value for nested tags using QXmlStreamReader. My xml is as follows:
<?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>
I just can't get values for tags Property whose are inside of other Property:
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();
}
What's wrong? Class Simple1 is readed and listed properly.
Found the solution myself - in case someone will come across similar issue here is working code:
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();
}
Got in console:
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"