Hi, everyone! I wanted to thank everybody for the help they've gave me while I've been on these forums. Now, it's my turn to contribute toward this community. I recently wrote a function to read from an XML file using a path. This supports attributes AND indexing. Here it is:
Qt Code:
  1. QString XmlUtils::GetData(QString path)
  2. {
  3. QDomDocument doc("myDocument");
  4. QFile *xmlFile = new QFile("file.xml");
  5. doc.setContent(xmlFile);
  6. QDomElement docElement = doc.documentElement();
  7. QDomNode node;
  8. node = docElement.firstChild();
  9.  
  10. QStringList pathFiles;
  11. QString returnString;
  12. pathFiles = path.split("/");
  13. int curindex = 0;
  14.  
  15. for(int i = 0; i < pathFiles.length(); i++)
  16. {
  17. QString curpath = pathFiles[i];
  18. if(curpath.startsWith('@'))
  19. {
  20. node = node.parentNode();
  21. curpath = curpath.remove('@');
  22. returnString = node.attributes().namedItem(curpath).nodeValue();
  23. }
  24. else
  25. {
  26. if(curpath.contains('['))
  27. {
  28. QStringList subPathFile = curpath.split('[');
  29. QString subIndex = subPathFile[1];
  30. subIndex = subIndex.remove(']');
  31. QString subNode = subPathFile[0];
  32. for(int x = 0; x < node.parentNode().childNodes().length(); x++)
  33. {
  34. if(node.nodeName() == subNode)
  35. {
  36. if(pathFiles[i] == pathFiles.back())
  37. {
  38. if(node.parentNode().childNodes().item(subIndex.toInt()).nodeName() == subNode)
  39. returnString = node.parentNode().childNodes().item(subIndex.toInt()).firstChild().nodeValue();
  40. break;
  41. }
  42. else
  43. node = node.parentNode().childNodes().item(subIndex.toInt());
  44. }
  45. }
  46. node = node.firstChild();
  47.  
  48. }
  49. else
  50. {
  51. for(int x = 0; x < node.parentNode().childNodes().length(); x++)
  52. {
  53. if(node.nodeName() == pathFiles[i])
  54. {
  55. if(pathFiles[i] == pathFiles.back())
  56. {
  57. if(node.nodeName() == pathFiles[i])
  58. returnString = node.firstChild().nodeValue();
  59. break;
  60. }
  61.  
  62. }
  63. else
  64. node = node.nextSibling();
  65. }
  66. node = node.firstChild();
  67. }
  68. }
  69.  
  70. }
  71. return returnString;
  72. }
To copy to clipboard, switch view to plain text mode 

How to use it:
Say you have the xml file:

<xml>
<Family>
<Foley>Justin</Foley>
<Foley>Ashley</Foley>
<Foley>Gavin</Foley>
</Family>
</xml>

And you want to get the value of 'Ashley.'
What you would do is:
QString name = XmlUtils->GetData("Family/Foley[1]");
The variable, 'name' would now be index [1] of the nodeset, 'Foley'. Please, enjoy and let me know if anybody does not understand. Praise is also accepted.