I have XML with structure like this:
<?xml version="1.0"?>
<Element id="123">
<Type>Type-of-element</Type>
<time>2020-02-20 20:20:20<time>
<part name="part1" type="Mech">Back</part>
<part name="part2" type="Mech">Back</part>
<part name="part3" type="Mech">Front</part>
<part name="part4" type="Electric">Side-left</part>
<part name="part5" type="Electric">Side-right</part>
<part name="part6" type="Electric">Front</part>
</Element>
I wrote this function to read this
QXmlStreamReader x(xml);
QString partName;
QString partType;
QString partValue;
unsigned long long int elementID;
QString type;
QString time;
if (x.readNextStartElement()){
if(x.name()=="Element"){
elementID=x.attributes().value("id").toULongLong();
qDebug()<<elementID;
}
while(x.readNextStartElement()){
if(x.name()=="part"){
partName = x.attributes().value("name").toString();
partType = x.attributes().value("type").toString();
partValue = x.readElementText();
qDebug()<<partName<<" "<<partType<<" "<<partValue;
}
else if(x.name()=="Type"){
type=x.readElementText();
qDebug()<<type;
}
else if(x.name()=="time"){.
time=x.readElementText();
qDebug()<<time;
}
else{
qDebug()<<"invalid name: "<<x.name();
}
}
}
And console print just this:
123 "Type-of-element" "20020-02-20 20:20:20"
So multiple attributes reading did not work. How I can read this?
It was a missing / in < /time> now it works perfectly:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.