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?
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";
}