Results 1 to 4 of 4

Thread: XML Dom parsing function

  1. #1
    Join Date
    Apr 2010
    Posts
    37
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb XML Dom parsing function

    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.

  2. #2
    Join Date
    Apr 2010
    Posts
    37
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML Dom parsing function

    add the code:
    Qt Code:
    1. node = node.firstChild();
    To copy to clipboard, switch view to plain text mode 
    right after line: 22. This will support early attributing. I.E. Family/@Foley/Foley[0]/@First if your XML looks like this:

    Qt Code:
    1. <xml>
    2. <Family Name="Foley">
    3. <Foley First="Justin">Justin</Foley>
    4. <Foley First="Ashley">asdf</Foley>
    5. <Foley>Gavin</Foley>
    6. </Family>
    7. </xml>
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML Dom parsing function

    Did you notice QXmlQuery? It makes exactly what your custom function does...

  4. #4
    Join Date
    Apr 2010
    Posts
    37
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML Dom parsing function

    No, I did not. Thank you for enlightening me.

    UPDATED CODE:

    Qt Code:
    1. QString XmlUtils::GetData(QString path)
    2. {
    3. QDomDocument doc("myDocument");
    4. QFile *xmlFile = new QFile("personaldata.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.  
    14. for(int i = 0; i < pathFiles.length(); i++)
    15. {
    16. QString curpath = pathFiles[i];
    17. if(curpath.startsWith('@'))
    18. {
    19. curpath = curpath.remove('@');
    20. QString rightNode = pathFiles[i-1];
    21. if(rightNode.contains('['))
    22. {
    23. QStringList rightNodeChildren = rightNode.split('[');
    24. rightNode = rightNodeChildren[0];
    25. }
    26. if(node.nodeName() != rightNode)
    27. node = node.parentNode();
    28. return node.attributes().namedItem(curpath).nodeValue();
    29. }
    30. else
    31. {
    32. if(curpath.contains('['))
    33. {
    34. QStringList subPathFile = curpath.split('[');
    35. QString subIndex = subPathFile[1];
    36. subIndex = subIndex.remove(']');
    37. QString subNode = subPathFile[0];
    38. for(unsigned int x = 0; x < node.parentNode().childNodes().length(); x++)
    39. {
    40. if(node.nodeName() == subNode)
    41. {
    42. if(pathFiles[i] == pathFiles.back())
    43. {
    44. if(node.parentNode().childNodes().item(subIndex.toInt()).nodeName() == subNode)
    45. // return node.parentNode().childNodes().item(subIndex.toInt()).nodeName();
    46. returnString = node.parentNode().childNodes().item(subIndex.toInt()).firstChild().nodeValue();
    47. break;
    48. }
    49. else
    50. node = node.parentNode().childNodes().item(subIndex.toInt());
    51. }
    52. }
    53. if(node.hasChildNodes())
    54. node = node.firstChild();
    55.  
    56. }
    57. else
    58. {
    59. for(unsigned int x = 0; x < node.parentNode().childNodes().length(); x++)
    60. {
    61. if(node.nodeName() == pathFiles[i])
    62. {
    63. if(pathFiles[i] == pathFiles.back())
    64. {
    65. if(node.nodeName() == pathFiles[i])
    66. returnString = node.firstChild().nodeValue();
    67. break;
    68. }
    69.  
    70. }
    71. else
    72. node = node.nextSibling();
    73. }
    74. if(node.hasChildNodes())
    75. node = node.firstChild();
    76. }
    77. }
    78.  
    79. }
    80. return returnString;
    81. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 10th March 2010, 08:13
  2. DTD Parsing in qt4?
    By darshan.hardas in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2009, 10:39
  3. XML parsing
    By systemz89 in forum Qt Programming
    Replies: 4
    Last Post: 29th December 2007, 18:31
  4. XML Parsing in Qt 3.3
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2007, 18:54
  5. Xml parsing
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2006, 11:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.