QXmlStreamReader does not parse XML attributes and attribute values
-
Hi. So, my issue is quite simple I'd say, I'm just trying to parse an XML file with QXmlStreamReader. So far so good, except I can't parse attributes and their values. I can parse everything else, like tags and elements, which I did, but I can't parse these attributes.
So, my code is something like this:
QFile xml_file("burma14.xml");
xml_file.open(QIODevice::ReadOnly | QIODevice::Text);QXmlStreamReader reader(&xml_file); QDomDocument doc("burma14"); while(!reader.atEnd()) { reader.readNext(); if (!reader.isStartElement()) { continue; } QString text = reader.name().toString(); if(text == QStringLiteral("vertex")) { this->v++; std::cout<<"Vertex: "<<this->v<<std::endl; } if(text == QStringLiteral("edge")) { std::cout<<"Edge to vertex: "<<reader.readElementText().toInt()<<std::endl; QXmlStreamAttributes attribs = reader.attributes(); QString attr = attribs.value("cost").toString(); std::cout<<"Cost: "<<attr.toStdString()<<std::endl; } }
Everything you see here works except for this part:
QXmlStreamAttributes attribs = reader.attributes();
QString attr = attribs.value("cost").toString();
std::cout<<"Cost: "<<attr.toStdString()<<std::endl;It simply returns nothing. Maybe I'm doing it wrong? Is there a better way to do it? Thanks in advance for your time.
-
Hi. So, my issue is quite simple I'd say, I'm just trying to parse an XML file with QXmlStreamReader. So far so good, except I can't parse attributes and their values. I can parse everything else, like tags and elements, which I did, but I can't parse these attributes.
So, my code is something like this:
QFile xml_file("burma14.xml");
xml_file.open(QIODevice::ReadOnly | QIODevice::Text);QXmlStreamReader reader(&xml_file); QDomDocument doc("burma14"); while(!reader.atEnd()) { reader.readNext(); if (!reader.isStartElement()) { continue; } QString text = reader.name().toString(); if(text == QStringLiteral("vertex")) { this->v++; std::cout<<"Vertex: "<<this->v<<std::endl; } if(text == QStringLiteral("edge")) { std::cout<<"Edge to vertex: "<<reader.readElementText().toInt()<<std::endl; QXmlStreamAttributes attribs = reader.attributes(); QString attr = attribs.value("cost").toString(); std::cout<<"Cost: "<<attr.toStdString()<<std::endl; } }
Everything you see here works except for this part:
QXmlStreamAttributes attribs = reader.attributes();
QString attr = attribs.value("cost").toString();
std::cout<<"Cost: "<<attr.toStdString()<<std::endl;It simply returns nothing. Maybe I'm doing it wrong? Is there a better way to do it? Thanks in advance for your time.
-
Hi,
First strange thing: you are using a relative path which means that the file in question will likely not be where you expect it to be..
Then you don't check whether the opening did succeed so you might trying to parse an empty document.
-
I see. I fixed those two issues, but the main problem seems to persist. Even though this way worked for others.
-
Did you check whether there was an error with QXmlStreamReader::hasError ?
-
I did, didn't print any error.
-
Hi. So, my issue is quite simple I'd say, I'm just trying to parse an XML file with QXmlStreamReader. So far so good, except I can't parse attributes and their values. I can parse everything else, like tags and elements, which I did, but I can't parse these attributes.
So, my code is something like this:
QFile xml_file("burma14.xml");
xml_file.open(QIODevice::ReadOnly | QIODevice::Text);QXmlStreamReader reader(&xml_file); QDomDocument doc("burma14"); while(!reader.atEnd()) { reader.readNext(); if (!reader.isStartElement()) { continue; } QString text = reader.name().toString(); if(text == QStringLiteral("vertex")) { this->v++; std::cout<<"Vertex: "<<this->v<<std::endl; } if(text == QStringLiteral("edge")) { std::cout<<"Edge to vertex: "<<reader.readElementText().toInt()<<std::endl; QXmlStreamAttributes attribs = reader.attributes(); QString attr = attribs.value("cost").toString(); std::cout<<"Cost: "<<attr.toStdString()<<std::endl; } }
Everything you see here works except for this part:
QXmlStreamAttributes attribs = reader.attributes();
QString attr = attribs.value("cost").toString();
std::cout<<"Cost: "<<attr.toStdString()<<std::endl;It simply returns nothing. Maybe I'm doing it wrong? Is there a better way to do it? Thanks in advance for your time.
-
That fixed the problem, thank you.