Qt work notes - Qt document reading notes - Official parsing of qualifiedName() and XML usage namespace

Posted by alexz on Sun, 29 Dec 2019 15:45:02 +0100

Catalog

Official analysis

Examples of bloggers

Official analysis

QXmlStreamReader::qualifiedName()
Returns the restricted name of the StartElement or EndElement;
The restriction name is the original name of the element in the XML data. It has a prefix and a suffix. The element of the suffix is called the local name. Because the prefix of a namespace is not unique (the same prefix can refer to different namespaces, and different prefixes can refer to the same namespace), instead of using qualifiedName(), use namepaceUri() and local name() in attributes.

Examples of bloggers

For example, when the xml code is as follows

<?xml version="1.0" encoding="GBK"?>

<root>
	<Node name="first Tree" text="hello"/>
	<Tree name="Tree addition" text="how are you?" msg="how old are you?"/>
	<Other xmlns:One="namespace">
		<One:a name="One a"/>
		<One:b name="One b"/>
	</Other>
</root>

The following code can be used for analysis (here, print is used for the model):

#include <QXmlStreamReader>
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QFile file("E:\\Qt2018\\qualifiedNameInXML\\my.xml");
    if(!file.open(QFile::ReadOnly|QFile::Text)){
        qDebug()<<"open failed!";
        return 0;
    }

    QXmlStreamReader reader;
    reader.setDevice(&file);

    while(!reader.atEnd()){
        reader.readNext();
        if(reader.name().toString()=="Node"){
            qDebug()<<reader.attributes().value("name").toString();
            qDebug()<<reader.attributes().value("text").toString();
        }
        else if(reader.prefix().toString()=="One"){
            qDebug()<<reader.qualifiedName();
            qDebug()<<reader.attributes().value("name").toString();
            qDebug()<<reader.attributes().value("text").toString();
            qDebug()<<reader.attributes().value("msg").toString();
        }

    }
    file.close();

    return 0;
}

The screenshot of the program is as follows:

Topics: xml encoding