1

I have several xml documents with the following style, but each of them containing different nodes.

<Protocol version = "1.02">
    <property name = "Name">
         <QString>Example</QString>
    </property>
    <property name = "Description">
         <QString/>
    </property>
    <property name = "This goes deep">
         <class type = "Deeper">
             <property name = "Deep name">
                 <QString> data </QString>
             </property>

             // ...

         </class>
    </property>

I'm parsing this with QDomElement/QDomNode. I need to get the attributes and the node values out of the tree (which is as I said before, unknown, and we are talking about hundreds of files). I have no problem getting the values:

QString MMainForm::processXmlElement( const QDomElement& aElement ) const
{
    QString ret =""; 

    QDomNode node = aElement.firstChild();
    while ( !node.isNull() )
    {
        QDomElement e = node.toElement();
        if ( node.childNodes().size() > 0 )
        {
            ret += processXmlElement( e );
        }
        else
        {
            ret += node.nodeValue() + "\n";
        }
        node = node.nextSibling();
    }

    return ret;
}

But getting the attributes (for example the text "Deep name") really stumbles me. I know there is an ::attributes() function for QDomElement but the resulting map always seems to contain only an empty string. The following code shows the concept of how I have been trying to extract the attributes:

QString MMainForm::getAttributes( const QDomElement& aElement ) const
{
    QString ret = "";

    auto var = aElement.attributes();
    for ( int i=0; i < var.count(); ++i)
    {
        ret += var.item( i ).toText().data() + '\n';
    }

    return ret;
}

Is this conceptionally correct? How would you go about extracting all the attributes into a nice list/string?

Á. Márton
  • 497
  • 1
  • 3
  • 21
  • Your approach looks correct. What `QDomElement::nodeType ()` function returns for your element in `MMainForm::getAttributes()` function? – vahancho Mar 09 '18 at 07:47
  • FYI as stated in the documentation for [Qt XML](http://doc.qt.io/qt-5/qtxml-module.html), which contains Qt's DOM parser, "The module is not actively maintained anymore. Please use the QXmlStreamReader and QXmlStreamWriter classes in Qt Core instead." – MrEricSir Mar 09 '18 at 18:09

1 Answers1

1

Since you iterate over a collection of QDomNode's of the same type (QDomAttr), you can call the nodeName() and nodeValue() methods on each item:

auto var = aElement.attributes();
for ( int i=0; i < var.count(); ++i)
{
    ret += var.item(i).nodeName() + ": " + var.item(i).nodeValue() + '\n';
}

Or you can cast each item to a QDomAttr and use name and value properties:

auto var = aElement.attributes();
for ( int i=0; i < var.count(); ++i)
{
    QDomAttr attribute = var.item(i).toAttr();
    ret += attribute.name() + ": " + attribute.value() + "\n";
}
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35