+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: How to parse this xml file?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    378
    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,476
    Thanks
    13
    Thanked 1,566 Times in 1,496 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
    378
    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,329
    Thanks
    3
    Thanked 5,004 Times in 4,783 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    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
    378
    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,476
    Thanks
    13
    Thanked 1,566 Times in 1,496 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
    378
    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,329
    Thanks
    3
    Thanked 5,004 Times in 4,783 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    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
    378
    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,476
    Thanks
    13
    Thanked 1,566 Times in 1,496 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
    378
    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 += " ";
    }

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

    Default Re: How to parse this xml file?

    In your code you are destroying the xml structure, instead using a flat list of tags. You need to recursively walk down the DOM tree.
    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.


  13. #13
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    378
    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?

    Could you modify my code.. how it should be like?

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

    Default Re: How to parse this xml file?

    Find the top-most "folder" tag, then read its attribute, ask for the first child tag, if it is folder or file then process it, otherwise ignore it. Then proceed to the next sibling and keep doing the same until you reach the end of hierarchy.

    QDomNode::firstChild(), QDomNode::firstChildElement(), QDomNode::nextSibling(), QDomNode::nextSiblingElements() and QDomNode::toElement() are your friends.

    There is also a simple example in QDomElement docs.
    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.


  15. #15
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    378
    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?

    How to find the top most tag and process as i mentioned in the output in the previous comment. I am getting the same output..

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

    I have added this code.. but am not getting the required output as mentioned.. Could you help me? I am getting releasesfppro and file as 22345 22566 1184.. and it comes out of the loop.. i cant see other values.
    Last edited by Gokulnathvc; 11th September 2012 at 08:43.

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

    Default Re: How to parse this xml file?

    You are still processing a flat list of tags. And you'll be doing that all the time until you get rid of "elementsByTagName" which gives you a flat list of elements. Please see the example in QDomElement.
    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.


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

    Default Re: How to parse this xml file?

    You will continue to get the same output if you continue to do the same things... so try something different. Wysota's list of documentation does not include elementsByTagName() because it gives you all the folder elements descended from a parent... and you do not want them all at once.

    If you want the first element perhaps a function with the word "first" in its name would be a good place to start.

  18. #18
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    378
    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?

    Could you edit my code and give a sample code. How I should process it?

  19. #19
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    378
    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?

    Qt Code:
    1. for ( node = docElem.firstChild(); !node.isNull();node = node.nextSibling()) {
    2.  
    3. while(node.toElement().tagName() == "folder")
    4. {
    5. firmFolder += node.toElement().attribute("name");
    6. firmFolder += "/";
    7. node = node.firstChild();
    8.  
    9. }
    10. while(node.toElement().tagName() == "file")
    11. {
    12. firmFiles += node.toElement().attribute("name");
    13. firmFiles += " ";
    14. node = node.nextSibling();
    15.  
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    I have added this code and it worked for the current structure. However it stops after one execution. I couldnt get the second values of folder and file also. third values of folder and file.

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

    Default Re: How to parse this xml file?

    Analyze your own code. Take a piece of paper, a pencil and go step by step through your algorithm and you'll find out why it stops after finding the first file.
    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.


+ Reply to Thread

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