我需要一种方法来使用 XMLStreamReader 在 QT 中获取 svg 文件中的值。我该怎么做?
这是 SVG 文件中的值。
这是我已经尝试过的方法。如果我得到一些东西,我会尝试打印出这些值。顺便说一句,我是 QT 的新手。
xmlFile = new QFile("composition.svg");
if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text))
qDebug() << ("Load XML File Problem");
xmlReader = new QXmlStreamReader(xmlFile);
//Parse the XML until we reach end of it
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
// Read next element
QXmlStreamReader::TokenType token = xmlReader->readNext();
//If token is just StartDocument - go to next
if(token == QXmlStreamReader::StartDocument) {
continue;
}
//If token is StartElement - read it
if(token == QXmlStreamReader::StartElement) {
if(xmlReader->name() == "g") {
continue;
}
if(xmlReader->name() == "g") {
qDebug() << xmlReader->readElementText();
//fprintf(stderr, xmlReader->readElementText().toLatin1());
}
}
}
最佳答案
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
// Read next element
QXmlStreamReader::TokenType token = xmlReader->readNext();
//If token is just StartDocument - go to next
if(token == QXmlStreamReader::StartDocument)
continue;
//If token is StartElement - read it
if(token == QXmlStreamReader::StartElement)
{
if(xmlReader->name() == "g")
continue;
if(xmlReader->name() == "polygon")
{
foreach(const QXmlStreamAttribute &attr, xmlReader->attributes())
{
if (attr.name().toString() == QLatin1String("style"))
{
qDebug() << attr.value().toLatin1();
}
}
}
}
}
我需要循环属性/节点来获取它的一些值。
关于c++ - 如何使用 QT 从 SVG 文件中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353502/