How to get values of child nodes with QDomDocument?

Tamás Szelei picture Tamás Szelei · Aug 24, 2010 · Viewed 12.4k times · Source

A recieve a string like this:

<invoke name="CanClose" returntype="xml">
   <arguments>
       <string># 998.40</string>
       <number>49920</number>
   </arguments>
</invoke>

I'd like to use QDomDocument to get the values of arguments' child nodes by their index (I would like to extract the strings "# 998.40" and "49920" in the example).

This is what I tried:

QString argument(int index)
{
    QDomNode arg = xml->elementsByTagName("arguments").at(index);
    return arg.nodeValue();
}

But even arg was empty. What am I doing wrong here?

Thanks in advance.

Answer

Tam&#225;s Szelei picture Tamás Szelei · Aug 26, 2010

So apparently the text inside a node is a node itself, too. This is how it works:

QString argument(int index)
{
    QDomNode arg = xml->firstChild().namedItem("arguments");
    return arg.childNodes().at(index).firstChild().nodeValue();
}