Results 1 to 1 of 1

Thread: Parse xml file using QStreamReader

  1. #1
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parse xml file using QStreamReader

    Hello everybody,

    I'm trying to parse a Xml file using QStreamReader, first time, I put the code in the main function like this:
    Qt Code:
    1. QFile *file = new QFile(":/source/qk.xml");
    2. //Test the qk.xml is opened or not..
    3. if(!file->open(QIODevice::ReadOnly | QIODevice::Text))
    4. {
    5. QMessageBox :: information(NULL,"XML-information","Xml open faile.",
    6. QMessageBox::Yes | QMessageBox :: No,
    7. QMessageBox::Yes);
    8. }
    9.  
    10. QXmlStreamReader xml(file);
    11.  
    12. while(!xml.atEnd())
    13. {
    14. if(xml.isStartElement() && xml.name()=="salary")
    15. {
    16. QXmlStreamAttributes attrs = xml.attributes();
    17. //---
    18. QStringRef fName = attrs.value("normalsalary");
    19. QStringRef sName = attrs.value("otsalary");
    20.  
    21. if(fName.isEmpty())
    22. {
    23. xml.readNext();
    24. continue;
    25. }
    26. QString bb="Normal-Salary is ";
    27. bb.append(fName.toString());
    28. bb.append(",OT-Salary is ");
    29. bb.append(sName.toString());
    30. QMessageBox::information(NULL,"XML-information",bb,QMessageBox::Ok);
    31. }
    32. else if(xml.isEndElement() && xml.name() == "persons")
    33. {
    34. //if EndElement is persons then stop the loop and create UI
    35. }
    36. xml.readNext();
    37. }
    38. */
    To copy to clipboard, switch view to plain text mode 
    the reference xml file like this:
    <?xml version="1.0" encoding="UTF-8" ?>
    <setting>

    <salary normalsalary="800" otsalary="1500" />

    </setting>

    until here,all the thing works prefectly, and then I put the Parse function in a class named XmlManager,However ,when I test the Class, It return me an error -"Premature end of document"
    there is the fallow class function GetSettingValue()
    Qt Code:
    1. QString XmlManager::GetSettingValue(QString mothernode, QString childnode)
    2. {
    3.  
    4. QFile *file = new QFile(":/source/qk.xml");
    5.  
    6. QXmlStreamReader xml(file);
    7. QString ww;
    8.  
    9.  
    10. while(!xml.atEnd())
    11. {
    12. if(xml.isStartElement() && xml.name()=="salary")
    13. {
    14. QXmlStreamAttributes attrs = xml.attributes();
    15. //---
    16. QStringRef fName = attrs.value("normalsalary");
    17. QStringRef sName = attrs.value("otsalary");
    18. i=1000;
    19. if(fName.isEmpty())
    20. {
    21. xml.readNext();
    22. continue;
    23. }
    24. }
    25. else if(xml.isEndElement() && xml.name() == "persons")
    26. {
    27. //if EndElement is persons then stop the loop and create UI
    28. }
    29. xml.readNext();
    30. }
    31. ww=xml.errorString();
    32. return ww;
    33. }
    To copy to clipboard, switch view to plain text mode 

    can any body give me a suggestion? Thanks.


    Added after 42 minutes:


    Sorry everybody, I found i make a stupid question, the reason I can't Parse the xml's context using a class is that ,I just never open the file , which I just forgot 'file->open..' ,,Oh,It waste me a whole day.....
    Last edited by Gary.zhang; 10th January 2011 at 04:00.

Similar Threads

  1. Parse Text File with Qt
    By rhf417 in forum Qt Programming
    Replies: 12
    Last Post: 26th June 2013, 03:47
  2. Best way to load and parse an HTML file ??
    By tuthmosis in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2008, 11:06
  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.