xmlqtqxmlstreamreader

How can I read the atributes of XML nodes in Qt?


I need to read the a,b,c values(wheqre a,b,c are the name of some atributes) foreach node and list them in a table, but I don't know how to read the node values. Could you give an example code, please?

Thanks in advance


Solution

  • QFile file("file.xml");
    if (file.open(QIODevice::ReadOnly)) {
        QXmlStreamReader reader(file.readAll());
        file.close();
        while(!reader.atEnd()) {
            reader.readNext();
            if (reader.isStartElement()) {
                if (reader.name() == "node_name") {
                    foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
                        if (attr.name().toString() == QLatin1String("attribute_name")) {
                            QString attribute_value = attr.value().toString();
                            // do something
                        }
                    }
                }
            }
        }
    }