Results 1 to 7 of 7

Thread: How to read a xml file in qt?

  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 read a xml file in qt?

    Help me in giving a sample or simple application on how to read the contents of the xml file in QT
    ?

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

    Default Re: How to read a xml file in qt?

    Qt Code:
    1. int main() {
    2. QFile file("file.xml");
    3. file.open(QIODevice::ReadOnly|QIODevice::Text);
    4. doc.setContent(&file);
    5. return 0;
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.


  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 read a xml file in qt?

    It says QDomDocument has incomplete type name.

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

    Default Re: How to read a xml file in qt?

    Too bad. After writing over 200 posts you should know how to include header files and enable Qt modules.
    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 read a xml file in qt?

    I have added header file and class. still it says this error

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to read a xml file in qt?

    Also include the QDomNode and QDomElement (not sure if this is the issue, but you will probably use that classes) and don't forget to add QT += xml in the .pro file (then rebuild).

  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 read a xml file in qt?

    The following code works fine.
    Qt Code:
    1. QFile* file = new QFile("C:\\persons.xml");
    2. QFile txtfile("xmldata.txt");
    3.  
    4. /* If we can't open it, let's show an error message. */
    5. if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
    6. QMessageBox::critical(this,
    7. "QXSRExample::parseXML",
    8. "Couldn't open example.xml",
    9. return;
    10. }
    11. /* QXmlStreamReader takes any QIODevice. */
    12. QXmlStreamReader xml(file);
    13. while (!xml.atEnd() && !xml.hasError())
    14. {
    15. xml.readNext();
    16. if (xml.isStartElement())
    17. {
    18. QString name = xml.name().toString();
    19. if (name == "firstname" || name == "surname" ||
    20. name == "email" || name == "website")
    21. {
    22. txtfile.open(QIODevice::WriteOnly | QIODevice::Text);
    23. txtfile.write("Element Name:");
    24. txtfile.write("\r\n");
    25. txtfile.write(name.toStdString().c_str());
    26. txtfile.write("\r\n");
    27. txtfile.write("Text:");
    28. txtfile.write("\r\n");
    29. txtfile.write(xml.readElementText().toStdString().c_str());
    30. txtfile.write("\r\n");
    31. }
    32. }
    33. }
    34. if (xml.hasError())
    35. {
    36. QMessageBox::warning(NULL,"Error",xml.errorString(),QMessageBox::Ok);
    37. }
    38. else if (xml.atEnd())
    39. {
    40. QMessageBox::warning(NULL,"End","Reached End!!!",QMessageBox::Ok);
    41. }
    42.  
    43. txtfile.close();
    44. file->close();
    To copy to clipboard, switch view to plain text mode 

    XML file is

    <?xml version="1.0" encoding="iso-8859-1" ?>
    <persons>
    <person>
    <firstname>John</firstname>
    <surname>Doe</surname>
    <email>john.doe@example.com</email>
    <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person>
    <person>
    <firstname>Jane</firstname>
    <surname>Doe</surname>
    <email>jane.doe@example.com</email>
    <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person>
    <person>
    <firstname>Matti</firstname>
    <surname>Meikäläinen</surname>
    <email>matti.meikalainen@example.com</email>
    <website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
    </person>
    </persons>

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. Read from a zip file
    By rajji_saini in forum Qt Programming
    Replies: 2
    Last Post: 21st June 2011, 00:28
  3. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 15:42
  4. Replies: 1
    Last Post: 20th June 2008, 18:43
  5. Read An Xml File
    By Alienxs in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2007, 00: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
  •  
Qt is a trademark of The Qt Company.