QXmlStreamReader 获取标签值
QXmlStreamReader get tag value
我在使用 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();
}
怎么了? Class Simple1 被正确读取和列出。
我自己找到了解决方案 - 如果有人会遇到类似的问题,这里是工作代码:
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"
我在使用 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();
}
怎么了? Class Simple1 被正确读取和列出。
我自己找到了解决方案 - 如果有人会遇到类似的问题,这里是工作代码:
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"