Results 1 to 2 of 2

Thread: help in reading XML file

  1. #1

    Default help in reading XML file

    Hi All

    I am just a beginner of Qt. So Pls excuse me if I have made some silly errors. I was trying to parse a simple XML file and wanted to show it on a simple interface. I have got the code here...

    test.cpp

    Qt Code:
    1. #include<qfile.h>
    2. #include<qdom.h>
    3. #include<iostream.h>
    4.  
    5. QFile file("simple.xml");
    6. if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    7. {
    8. qDebug("Failed to open file for reading");
    9. return -1;
    10. }
    11.  
    12. QDomDocument document;
    13. if(!document.setContent(&file))
    14. {
    15. qDebug("Failed to parse the file into a DOM tree");
    16. file.close();
    17. return -1;
    18. }
    19.  
    20. file.close();
    21.  
    22. QDomElement documentElement = document.documentElement();
    23. QDomNode node = document.firstChild();
    24. while(!node.isNull())
    25. {
    26. if(node.isElement())
    27. {
    28. QDomElement element = node.toElement();
    29. cout<<"Element"<<element.tagName();
    30. cout<<"Element attribute name"<<element.attribute("name", "not set")
    31. }
    32.  
    33.  
    34. if (node.isText())
    35. {
    36. QDomText text = node.toText();
    37. qDebug << text.data();
    38. }
    39.  
    40. node=node.nextSibling();
    41. }
    To copy to clipboard, switch view to plain text mode 

    This is the error which I got.

    Qt Code:
    1. test.cpp:6: error: expected unqualified-id before ‘if’
    2. test.cpp:13: error: expected unqualified-id before ‘if’
    3. test.cpp:20: error: expected constructor, destructor, or type conversion before ‘.’ token
    4. test.cpp:24: error: expected unqualified-id before ‘while’
    5. make: *** [test.o] Error 1
    To copy to clipboard, switch view to plain text mode 
    and here is the XML file

    Qt Code:
    1. <document name="DocName">
    2. <author name="AuthorName" />
    3. </document>
    To copy to clipboard, switch view to plain text mode 
    If anyone could help it would be absolute gr8!!!!

    Cheers
    Shiva
    Last edited by jpn; 24th March 2008 at 14:10. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: help in reading XML file

    I'm not in mood to get into details but I think all this is supposed to be part of a function
    Quote Originally Posted by cshiva_in View Post

    QFile file("simple.xml");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    qDebug("Failed to open file for reading");
    return -1;
    }

    QDomDocument document;
    if(!document.setContent(&file))
    {
    qDebug("Failed to parse the file into a DOM tree");
    file.close();
    return -1;
    }

    file.close();

    QDomElement documentElement = document.documentElement();
    QDomNode node = document.firstChild();
    while(!node.isNull())
    {
    if(node.isElement())
    {
    QDomElement element = node.toElement();
    cout<<"Element"<<element.tagName();
    cout<<"Element attribute name"<<element.attribute("name", "not set")
    }


    if (node.isText())
    {
    QDomText text = node.toText();
    qDebug << text.data();
    }

    node=node.nextSibling();
    }

Similar Threads

  1. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 15:47
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. Replies: 3
    Last Post: 18th October 2007, 18:07
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.