tetra
tetra

Reputation: 197

to parse xml node children values under same tagname

I have two nodes PW_KeyGrip and PW_PalmerGrasp and the node children names are same in both the nodes.

<PW_KeyGrip>
  <PW_p1_EDC>173</PW_p1_EDC>
  <PW_p2_EDC>78</PW_p2_EDC>
  <PW_p2_FDS>196</PW_p2_FDS>
 </PW_KeyGrip>
 <PW_PalmerGrasp>
  <PW_p1_EDC>202</PW_p1_EDC>
  <PW_p1_APB>118</PW_p1_APB>
  <PW_p2_EDC>86</PW_p2_EDC>
 </PW_PalmerGrasp>

earlier, I used to search by tag name in order to select the value as:

root.elementsByTagName("PW_p1_EDC").at(0).firstChild().nodeValue()

Now since I have two values under same tag name. can you show me a way to select both the values under same tag name separately?

for example:

  1. How to select PW_p1_EDC under PW_KeyGrip
  2. How to select PW_p1_EDC under PW_PalmerGrasp

Upvotes: 0

Views: 34

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6584

You could use a simple function to retrieve the node value under a given tag name:

int findValue(QDomElement const& root, QString const& name)
{
    QDomElement element = root.firstChildElement(name);
    return element.firstChildElement("PW_p1_EDC").firstChild().nodeValue().toInt();
}

Then you would have to call it like that:

    qDebug() << "PW_KeyGrip: " << findValue(root, "PW_KeyGrip");
    qDebug() << "PW_PalmerGrasp: " << findValue(root, "PW_KeyGrip");

If you want to find the values without the exact path, you could take a look to XPath and QXmlQuery

Upvotes: 1

Related Questions