Qt World Summit: Register Today!

QDomNamedNodeMap, example of how to process attributes?

  • I've previously written my own classes in C++ to parse XML, however today I came across the Qt classes and I would like to use these. Can anyone explain how QDomNamedNodeMap is used to work with attributes as it seems very different to other ways of working in Qt in that there are no iterators to work through the attributes in the map.

    Also the map itself looks as the name suggests as if it was designed for nodes, so how do I use it to work through all attributes and extract the name and value of each?

  • Lifetime Qt Champion

    I'd go with nodeName and nodeValue.

  • Lifetime Qt Champion


    The attributes map is returned by QDomNode::attributes.

    Then it seems that you can use a simple for loop to read through the items of the map.

    Depending on what you need, the QXmlStreamReader classes.

    Note that I haven't used these classes since some times already.

  • @SGaist , thank you, yes I can see how to use, the contents of the class doesn't seem very relevant to attributes tho...looks as the name implies like its for nodes.

    In this example:

    QDomElement objNode = objXML.documentElement();
    while( !objNode.isNull() ) {
        QDomNamedNodeMap mpAttr = objNode.attributes();
        std::cout << objNode.tagName().toLatin1().data() << std::endl;
        for( int i=0; i<mpAttr.size(); i++ ) {
            QDomNode item(mpAttr.item(i));

    How would I obtain the attribute name and value from the item ?

    What I'm trying to do is parse any XML file then validate it and extract all nodes with any attributes, but it seems these classes were designed specifically with the user having to know the layout of the file they are going to read and what attributes will be in the file. There doesn't seem to be a way to parse all/any attributes for a node.

  • Lifetime Qt Champion

    I'd go with nodeName and nodeValue.

  • @SGaist , thank you, I'll take a look.

Log in to reply