Results 1 to 20 of 24

Thread: How to parse this xml file?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to parse this xml file?

    How to parse this xml file and get the folder name as release\sf\p1 and file name as setg and metg.??

    Qt Code:
    1. <package version="1.0">
    2. <tools>
    3. <charlie>
    4. <minversion>3.01.05</minversion>
    5. </charlie>
    6. <tango>
    7. <minversion>4.0.4</minversion>
    8. </tango>
    9. </tools>
    10. <folder name="release">
    11. <folder name="sf">
    12. <folder name="p1">
    13. <file name="setg">
    14. <version>1.4.1.2</version>
    15. <offset>0</offset>
    16. <attribute type="encrypted">1</attribute>
    17. </file>
    18. <file name="metg">
    19. <version>1.4.1.2</version>
    20. <offset>0</offset>
    21. <attribute type="encrypted">1</attribute>
    22. </file>
    23. </folder>
    24. </folder>
    25. </folder>
    26. </package>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to parse this xml file?

    Using the facilities of the QtXml or QtXmlPatterns module or QXmlStreamReader.

    How about you tell us what you have tried?

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse this xml file?

    I have tried with QDocDocument and I am always getting values as 1.4.1.2011.4.1.201?

    Qt Code:
    1. QList< QMap<QString,QString> > Package;
    2.  
    3. /* QDomDocument takes any QIODevice. as well as QString buffer*/
    4. QDomDocument doc("mydocument");
    5. if (!doc.setContent(xmlFile))
    6. {
    7. return;
    8. }
    9.  
    10. //Get the root element
    11. QDomElement docElem = doc.documentElement();
    12.  
    13. // you could check the root tag name here if it matters
    14. QString rootTag = docElem.tagName(); // == package
    15. QString fll = docElem.text();
    16.  
    17. // get the node's interested in, this time only caring about person's
    18. QDomNodeList nodeList = docElem.elementsByTagName("folder");
    19.  
    20. //Check each node one by one.
    21. for(int ii = 0;ii < nodeList.count(); ii++)
    22. {
    23. QMap<QString, QString> firmwarefolder;
    24.  
    25. // get the current one as QDomElement
    26. QDomElement el = nodeList.at(ii).toElement();
    27.  
    28. firmwarefolder["folder"] = el.attribute("folder"); // get and set the attribute ID
    29. firmwarefolder["file"] = el.attribute("file");
    30.  
    31. //get all data for the element, by looping through all child elements
    32. QDomNode pEntries = el.firstChild();
    33. while(!pEntries.isNull()) {
    34. QDomElement peData = pEntries.toElement();
    35. QString tagNam = peData.tagName();
    36.  
    37. if(tagNam == "folder") { /* We've found first name. */
    38. firmwarefolder["file"] = el.text();
    39. }else if(tagNam == "file") { /* We've found surname. */
    40. firmwarefolder["file"] = peData.text();
    41. }
    42.  
    43. pEntries = pEntries.nextSibling();
    44. }
    45.  
    46. Package.append(firmwarefolder);
    47. }
    To copy to clipboard, switch view to plain text mode 

    I have added these codes. But am not getting the folder name and file name.. Whats wrong with this?
    Last edited by wysota; 10th September 2012 at 09:05.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to parse this xml file?

    The attribute you are looking for is called "name" and not "folder".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse this xml file?

    I have tried with that and got the values, but still it is showing me different values not the output what i have mentioned above

    Qt Code:
    1. <folder name="release">
    2. <folder name="sf">
    3. <folder name="ppro">
    4. <file name="22345">
    5. <version>1.4.1.2</version>
    6. <offset>0</offset>
    7. <attribute type="encrypted">1</attribute>
    8. </file>
    9. <file name="22566">
    10. <version>1.4.1.2</version>
    11. <offset>0</offset>
    12. <attribute type="encrypted">1</attribute>
    13. </file>
    14. </folder>
    15. </folder>
    16. </folder>
    17. <folder name="release">
    18. <folder name="bios">
    19. <file name="1184">
    20. <version>1.4.1.2</version>
    21. <offset>0</offset>
    22. <attribute type="encrypted">1</attribute>
    23. </file>
    24. <file name="1084">
    25. <version>1.4.1.2</version>
    26. <offset>0</offset>
    27. <attribute type="encrypted">1</attribute>
    28. </file>
    29. </folder>
    30. </folder>
    To copy to clipboard, switch view to plain text mode 

    How to get the values in first loop as folder = releasesfppro and file = 2234522566 and in second loop as folder = releasebios file = 11841084???
    Last edited by Gokulnathvc; 11th September 2012 at 06:43.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to parse this xml file?

    You are getting the text of the <folder> and <file> elements that you are asking for at lines 38 and 40. If you want the value of the name attribute of these elements then you should ask for that.

  7. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse this xml file?

    I have updated the code and edited the values in the previous comment. Could you look at that?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to parse this xml file?

    Do you understand the difference between XML attribute value and XML tag text?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse this xml file?

    I am new to XML programming. Could you see my updated code and give me suggestion and a solution?

    Qt Code:
    1. QDomNodeList nodeList = docElem.elementsByTagName("folder");
    2. for(int k = 0;k < nodeList.count(); k++)
    3. {
    4. QDomElement el = nodeList.at(k).toElement();
    5. firmFolder += el.attribute("name");
    6. firmFolder += "/";
    7. }
    8.  
    9. nodeList = docElem.elementsByTagName("file");
    10. for(int l = 0;j < nodeList.count(); l++)
    11. {
    12. QDomElement el = nodeList.at(l).toElement();
    13. firmFiles += el.attribute("name");
    14. firmFiles += " ";
    15. }
    To copy to clipboard, switch view to plain text mode 


    I have added these codes. but am getting folder as release/sf/ppro/release/bios and file as 223452256611841084?
    But i should get this as release/sf/ppro/ and file 2234522566 for first loop.. and release/bios and file 11841084 for the second loop.. How to get this..
    Last edited by Gokulnathvc; 11th September 2012 at 07:21.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to parse this xml file?

    I am new to XML programming. Could you see my updated code and give me suggestion and a solution?
    The code has not been changed as far as I can see. There's still the obvious errors at line 38/40

    I have already told you what you need to query to retrieve the folder/file name from the <folder>/<file> element, and you are already using the relevant function. Wysota has already given you the name of the attribute you want. If you explain which part you do not understand we can help

  11. #11
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse this xml file?

    Kindly check the updated code..
    QDomNodeList nodeList = docElem.elementsByTagName("folder");
    for(int k = 0;k < nodeList.count(); k++)
    {
    QDomElement el = nodeList.at(k).toElement();
    firmFolder += el.attribute("name");
    firmFolder += "/";
    }

    nodeList = docElem.elementsByTagName("file");
    for(int l = 0;j < nodeList.count(); l++)
    {
    QDomElement el = nodeList.at(l).toElement();
    firmFiles += el.attribute("name");
    firmFiles += " ";
    }

Similar Threads

  1. How to parse QML file from QT C++
    By AbinaThomas in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2012, 09:47
  2. Parse huge XML file
    By juracist in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2012, 01:54
  3. Replies: 13
    Last Post: 21st June 2006, 21:22
  4. How to parse this XML file ?
    By probine in forum Qt Programming
    Replies: 7
    Last Post: 4th April 2006, 09:05
  5. Parse a pickle file
    By karye in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2006, 17:02

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.